从ProcessBuilder执行一个jar,给出了ClassNotFoundException

时间:2011-01-04 22:12:28

标签: java processbuilder

当我尝试从命令行执行jar时,它工作正常。我试图在另一个java程序中执行相同的jar,如下所示:

ProcessBuilder pb = new ProcessBuilder("java", "-Xmx1024m -Xms1024m -DTOOLS_DIR="/home/IM/work/dist" -Daoi=whole -jar "/home/IM/work/dist/idt_tools.jar");
pb.start()

但是,这给了我一个ClassNotFoundException

ERR>Exception in thread "main" java.lang.NoClassDefFoundError:  -Xmx1024m -Xms1024m -DTOOLS_DIR="/home/IM/work/dist" -Daoi=whole -jar "/home/IM/work/dist/idt_tools/jar"
ERR>Caused by: java.lang.ClassNotFoundException:  -Xmx1024m -Xms1024m -DTOOLS_DIR=".home.IM.work.dist" -Daoi=whole -jar ".home.IM.work.dist.idt_tools.jar"
ERR>    at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
ERR>    at java.security.AccessController.doPrivileged(Native Method)
ERR>    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
ERR>    at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
ERR>    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
ERR>    at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
ERR>Could not find the main class: -Xmx1024m -Xms1024m -DTOOLS_DIR="/home/IM/work/dist" -Daoi=whole -jar "/home/IM/work/dist/idt_tools.jar".  Program will exit.

为什么认为这是一堂课?你能否在一个长字符串中指定你的参数?

1 个答案:

答案 0 :(得分:6)

对我有意义。

也许您不打算运行名为“-Xmx1024m -Xms1024m -DTOOLS_DIR =”/ home / IM / work / dist“-Daoi = whole -jar”/ home / IM / work / dist / idt_tools的类。 “也许你的意思是那些分开的论点?

ProcessBuilder pb = new ProcessBuilder("java", "-Xmx1024m", "-Xms1024m",
    "-DTOOLS_DIR=/home/IM/work/dist", "-Daoi=whole", 
    "-jar", "/home/IM/work/dist/idt_tools.jar");