当我尝试从命令行执行 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.
为什么它认为这是一个类?您不能在一个长字符串中指定您的参数吗?