java创建exe程序快捷方式

java创建exe程序快捷方式

我们平时可以使用鼠标右击exe选择创建快捷方式,但是如何使用java代码创建exe的快捷方式呢?

比如说我用java写好一个程序打包成exe,这个程序可以实现开机自启动,那么我们就可以在内部代码中直接创建快捷方式然后自动复制到开机启动项目录即可实现!

开始撸代码,这里我们需要一个人家封装好的jar包,还需要一个dll文件,实际上就是加载动态库dll文件实现功能。

大家自行网上搜索:jshortcut.jar,dll:jshortcut.dll

使用方法也很简单,代码如下:

package createhortcut;

import java.io.File;

import net.jimmc.jshortcut.JShellLink;

public class CreateShortCut {

	public static void main(String[] args) {
		boolean isSucc = createLnk("E:\\Java\\workspace\\swing\\", "ScreenShot1.0.exe",
				"E:\\Java\\workspace\\swing\\sshot\\ScreenShot1.0.exe");
		System.out.println(isSucc);
	}

	public static boolean createLnk(String savePath, String appName, String exePath) {
		try {
			File f = new File(exePath);
			File f2 = new File(savePath);
			if (!f.exists() || !f2.exists()) {
				return false;
			}
			JShellLink link = new JShellLink();
			// 存放路径
			link.setFolder(savePath);
			// 快捷方式名称
			link.setName(appName);
			// 指向的exe
			link.setPath(exePath);
			link.save();
			return true;
		} catch (Throwable e) {
			// 是更改后的jar应用,直接全部抛出
			e.printStackTrace();
		}
		return false;
	}
}


实例化一个JShellLink对象link,设置存放路径link.setFolder(savePath);,设置快捷方式名称link.setName(appName);,设置指向的exe程序位置link.setPath(exePath);,最后保存link.save();即可。快捷方式就产生了!



就这样很简单的调用就好了。

但是当我把包含此代码的java程序打包成exe时,会报找不到dll文件的错,于是反编译此jar包,查看源码发现了小问题,当打包成exe后,程序会到exe同级文件夹下找dll文件,很是不方便,于是小改了一下代码,成功使用!

反编译代码如下:

static {
			try {

				String str1 = System.getProperty("JSHORTCUT_HOME");
				if (str1 != null) {
					localObject1 = new File(str1, "jshortcut.dll");
					String str2 = ((File) localObject1).getAbsolutePath();
					System.load(str2);
				} else {
					System.loadLibrary("jshortcut");
				}
			} catch (UnsatisfiedLinkError localUnsatisfiedLinkError) {
				Object localObject1 = System.getProperty("java.class.path");
				int i = 0;
				while (((String) localObject1).length() > 0) {
					int j = ((String) localObject1).indexOf(File.pathSeparator);
					Object localObject2;
					if (j >= 0) {
						localObject2 = ((String) localObject1).substring(0, j);
						localObject1 = ((String) localObject1).substring(j + 1);
					} else {
						localObject2 = localObject1;
						localObject1 = "";
					}
					if ((((String) localObject2).length() > 4) && (((String) localObject2)
							.substring(((String) localObject2).length() - 4).toLowerCase().equals(".jar"))) {
						j = ((String) localObject2).lastIndexOf(File.separator);
						if (j > 0) {
							localObject2 = ((String) localObject2).substring(0, j);
						} else {
							localObject2 = ".";
						}
					}
					File localFile = new File((String) localObject2, "jshortcut.dll");
					if (localFile.exists()) {
						String str3 = localFile.getAbsolutePath();
						System.load(str3);
						i = 1;
						break;
					}
				}
				if (i == 0) {
					throw localUnsatisfiedLinkError;
				}
			}

	}

这是部分代码,主要功能时加载dll文件,先去找环境变量,之后没有的话再进行下一步操作,经过参考其他文章,外加百度,解决如下,这里我们之间多加一个方法,将dll文件直接放到src文件夹下,让jar包直接去加载这个地方法dll文件,我们先创建一个java项目,按照jar中创建对应的包名和类,将代码复制进去。


更改如下:

...
static {
		if (getFile()) {

		} else {
			try {

				String str1 = System.getProperty("JSHORTCUT_HOME");
				if (str1 != null) {
					localObject1 = new File(str1, "jshortcut.dll");
					String str2 = ((File) localObject1).getAbsolutePath();
					System.load(str2);
				} else {
					System.loadLibrary("jshortcut");
				}
			} catch (UnsatisfiedLinkError localUnsatisfiedLinkError) {
				Object localObject1 = System.getProperty("java.class.path");
				int i = 0;
				while (((String) localObject1).length() > 0) {
					int j = ((String) localObject1).indexOf(File.pathSeparator);
					Object localObject2;
					if (j >= 0) {
						localObject2 = ((String) localObject1).substring(0, j);
						localObject1 = ((String) localObject1).substring(j + 1);
					} else {
						localObject2 = localObject1;
						localObject1 = "";
					}
					if ((((String) localObject2).length() > 4) && (((String) localObject2)
							.substring(((String) localObject2).length() - 4).toLowerCase().equals(".jar"))) {
						j = ((String) localObject2).lastIndexOf(File.separator);
						if (j > 0) {
							localObject2 = ((String) localObject2).substring(0, j);
						} else {
							localObject2 = ".";
						}
					}
					File localFile = new File((String) localObject2, "jshortcut.dll");
					if (localFile.exists()) {
						String str3 = localFile.getAbsolutePath();
						System.load(str3);
						i = 1;
						break;
					}
				}
				if (i == 0) {
					throw localUnsatisfiedLinkError;
				}
			}
		}

	}

	public static String getDirectory(String paramString) {
		return nGetDirectory(paramString.toLowerCase());
	}

	private static boolean getFile() {
		try {
			System.loadLibrary("jshortcut");
			return true;
		} catch (Throwable exLoadLibrary) {
			try {

				String jarPath = "net/jimmc/jshortcut/";
				String tmpDir = System.getProperty("java.io.tmpdir");
				try {
					String dll = "jshortcut.dll";
					fromJarToFs(jarPath + dll, tmpDir + dll);
					System.load(tmpDir + dll);
				} catch (UnsatisfiedLinkError e) {
					String dll = "jshortcut.dll";
					fromJarToFs(jarPath + dll, tmpDir + dll);
					System.load(tmpDir + dll);
				} catch (IOException e) {

				}
				return true;
			} catch (Throwable exAllFailed) {
			}
		}

		return false;
	}

	private static void fromJarToFs(String jarPath, String filePath) throws IOException {
		InputStream is = null;
		OutputStream os = null;
		try {
			File file = new File(filePath);
			if (file.exists()) {
				boolean success = file.delete();
				if (!success) {
					throw new IOException("Could not delete file: " + filePath);
				}
			}
			is = ClassLoader.getSystemClassLoader().getResourceAsStream(jarPath);
			os = new FileOutputStream(filePath);
			byte[] buffer = new byte['?'];
			int bytesRead;
			while ((bytesRead = is.read(buffer)) != -1) {
				os.write(buffer, 0, bytesRead);
			}
		} catch (Exception ex) {
			throw new IOException("FromJarToFileSystem could not load DLL: " + jarPath, ex);
		} finally {
			if (is != null) {
				is.close();
			}
			if (os != null) {
				os.close();
			}
		}
	}
...

再将此项目导出成jar即可。这样我们在使用此jar包创建快捷方式时,直接在项目中创建一个包net.jimmc.jshortcut,将dll放入


这样在打包成exe后也能正常使用啦!

源码:https://github.com/ricozhou/javacreateshortcut

猜你喜欢

转载自blog.csdn.net/rico_zhou/article/details/80062917
今日推荐