昨天我已经完成了开发我的Java应用程序,我有同样的问题。如果我双击.jar文件,或者我在命令行启动画面中执行完美,但是当我执行包装文件时,它不会。似乎只是JSmooth不支持这个功能。
但是我做了一个小技巧,让包装.exe
和闪屏同时工作。我做了一个名为ApplicationLoader.jar
的小应用程序,它包含一个主要的类,它们在命令行中执行java -jar "Application.jar"
。下面是完整的代码:
public class ApplicationLoader {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
/* First I check if the first parameter is not null and it's not an empty string */
if(args[0] != null && !args[0].trim().isEmpty()){
/* Then I use java.util.regex package to validate the parameter is a .jar file */
Pattern pattern = Pattern.compile(".*jar");
Matcher matcher = pattern.matcher(args[0]);
if(matcher.matches()){
/* Finally I define the command line like: java -jar "Application.jar" */
String command = "java -jar \"" + args[0] + "\"";
try {
Runtime r = Runtime.getRuntime();
ProcessBuilder pb = new ProcessBuilder("cmd.exe", "/c", command);
Process p = pb.start();
p.waitFor();
} catch (IOException | InterruptedException ex) {
JOptionPane.showMessageDialog(null, ex.getMessage(), "Error executing: "+command, JOptionPane.ERROR_MESSAGE);
}
} else {
JOptionPane.showMessageDialog(null, "The argument is not a .jar file!!");
}
} else {
JOptionPane.showMessageDialog(null, "There's not a valid argument!");
}
}
}
我有我的应用程序在此文件夹结构:
MyApp
+-- bin
| +-- MyApp.jar
| +-- ApplicationLoader.jar
+-- MyApp.exe
在
JSmoot
所以我改变类路径ApplicationLoader.jar
并添加相对位置,以我的应用程序in 申请参数这样的节:
![enter image description here](https://i.stack.imgur.com/WBikK.jpg)
就是这样。我知道这不是最好的选择,但是是一种解决方法。
但是有一个小问题:
由于ApplicationLoader.jar
调用cmd.exe
然后包裹.exe
和应用程序将在两个不同的过程执行。
这意味着如果您因为某种原因(意外崩溃或其他原因)而需要杀死.exe
进程,那么您的java应用程序仍然会作为java.exe
进程工作。所以在这种情况下,你必须杀死MyApp.exe
和java.exe
进程。其实如果你只是杀了java.exe
进程然后MyApp.exe
进程就会自行完成执行。
如果你牢记这一点,你可以忍受,我认为这个选项是非常简单和有用的。 我希望这有助于任何寻找解决此问题的解决方案的人。
“资源”文件夹和“LOADLOGO.png”,它们在哪里?也许,你需要告诉安装文件夹的结构。 –
我使用Launch4j生成了exe文件,它工作。皮蒂说JSmooth不能做到这一点。 –
是的,我也使用Launch4J,每次使用它时都会更可靠,还有InnoSetup + Ant任务 –