简体   繁体   English

从Java应用程序执行Jar文件

[英]Executing a Jar file from an Java application

I have 2 methods I wrote to try and execute a Jar file from my Java application and they both are not doing anything. 我写了2种方法来尝试从Java应用程序执行Jar文件,但它们都不做任何事情。 The Java Runtime Environment is installed on the C: drive and by default its Path points to a directory on the C: drive. Java运行时环境安装在C:驱动器上,默认情况下,其路径指向C:驱动器上的目录。 The Jar file I am trying to execute is located on the E: drive. 我要执行的Jar文件位于E:驱动器上。

Jar location: E:\\Demo Folder\\MyDemo.jar 罐子位置:E:\\ Demo文件夹\\ MyDemo.jar

I tried to execute MyDemo.jar using the following 2 methods: 我尝试使用以下2种方法执行MyDemo.jar:

Method 1: 方法1:

try {
    Runtime.getRuntime().exec("cmd /c start %SystemDrive%\\java -jar " + "E:/Demo Folder/MyDemo.jar");
} catch (IOException ex) {
    System.err.println(ex.getMessage());
}

Method 2: 方法2:

try {
    File dirFile = new File("E:/Demo Folder/");
    ProcessBuilder pb = new ProcessBuilder("java", "-jar", "E:/Demo Folder/MyDemo.jar");
    pb.directory(dirFile);
    Process p = pb.start();
} catch (Exception ex) {
    System.err.println(ex.getMessage());
}

Didn't you try to put your invocation logic inside a, say, E:/Demo Folder/rundemo.bat` (or .cmd) file, and call that .bat from your java instead? 您是否没有尝试将调用逻辑放在一个E:/ Demo Folder / rundemo.bat`(或.cmd)文件中,而是从Java中调用该.bat? That's usually more sane and easy to troubleshoot. 这通常更为理智,并且易于排除故障。

I'm guessing the problem is the space in the path of the jar file. 我猜问题出在jar文件的路径中的空间。 Try this: 尝试这个:

new ProcessBuilder("java", "-jar", "\"E:/Demo Folder/MyDemo.jar\"");

To launch a external java executable application you must first locate the java.exe file (Which loads the JVM) and pass the argument of -jar to indicate loading of a executable JAR file. 要启动外部Java可执行应用程序,您必须首先找到java.exe文件(将加载JVM)并传递-jar参数以指示已加载可执行JAR文件。 In both methods you provided you had minor errors within your code. 您提供的这两种方法在代码中都存在一些小错误。

In method one: 在方法一中:

cmd /c start %SystemDrive%\\\\java -jar cmd / c开始%SystemDrive%\\\\ java -jar

%SystemDrive% is being treated as a string literal as java is unaware of Windows specific environment variables. 由于Java不知道Windows特定的环境变量,因此%SystemDrive%被视为字符串文字。

In method two: 在方法二中:

"java", "-jar", "E:/Demo Folder/MyDemo.jar" “ java”,“-jar”,“ E:/ Demo文件夹/MyDemo.jar”

You are assuming that java.exe has been added to PATH environmental variables which may not be the case. 您假设java.exe已添加到PATH环境变量中,而事实并非如此。 Also, from your usage of the "%" operators, I am assuming you are on a windows machine, which uses \\ for directories therefore... "E:/Demo Folder/MyDemo.jar" may not return a valid location. 另外,根据您使用“%”运算符的情况,我假设您使用的Windows计算机上的\\用于目录,因此...“ E:/ Demo Folder / MyDemo.jar”可能不会返回有效位置。

Try the following segment of code: 尝试以下代码段:

try {
    File dirFile = new File("E:\\Demo Folder\\");
    ProcessBuilder pb = new ProcessBuilder(System.getProperty("java.home") + File.separator + "bin" + File.separator + "java", "-jar", new File(dirFile, "MyDemo.jar").getAbsolutePath());
    pb.directory(dirFile);
    Process p = pb.start();
} catch (Exception ex) {
    System.err.println(ex.getMessage());
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM