且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在Java程序中运行.jar文件?

更新时间:2022-01-09 02:41:11

使用绝对路径而不是相对路径,如果从任何工作目录运行,应该阻止找不到路径。否则将其添加到您的类路径中,如Nizil所说。

Use an absolute path instead of a relative path, that should prevent the path not being found if you run from any working directory. Otherwise add it to your classpath as Nizil said.

要获取当前用户的名称,请使用 System.getProperty(user.name)并连接到你的路径。

To get the current user's name, use System.getProperty("user.name") and concatenate into your path.

user = System.getProperty("user.name");
cmd = "java -jar C/Users/" + user + "/appdata/Roaming/<folder>/<file>.jar";
Runtime.getRuntime().exec(cmd);