从java文件运行jar文件

时间:2014-04-23 07:41:26

标签: java process jar

我正在尝试从java程序运行jar文件,我找到了这个链接:here。我使用以下代码:

ProcessBuilder pb = new ProcessBuilder("CEDDextractor_all_img.jar", "-jar", "cedd/");
    pb.directory(new File("cedd/"));
    Process p = pb.start();

但是我收到错误:无法加载图片:无法运行程序“cedd / CEDDextractor_all_img.jar”(在目录“cedd”中):CreateProcess error = 193,%1不是有效的Win32应用程序。我对我需要指定的路径感到困惑。

批量命令:

java -jar CEDDextractor_all_img.jar -file "file.jpg"

2 个答案:

答案 0 :(得分:1)

ProcessBuilder pb = new ProcessBuilder("java", "-jar", "CEDDextractor_all_img.jar", "cedd/");
pb.directory(new File("cedd/"));
Process p = pb.start();

可执行文件是java :-)(也可能是javaw

答案 1 :(得分:1)

仅仅调用" java"是不正确的,这可能不在PATH中;你应该使用:

String jarPath = ...;
ProcessBuilder pb = new ProcessBuilder(System.getProperty("java.home")
        + File.separator
        + "bin"
        + File.separator
        + "java", "-jar", jarPath);
Process p = pb.start();