从 ProcessBuilder 执行 jar 给出 ClassNotFoundException

发布于 2024-10-10 05:36:18 字数 1281 浏览 7 评论 0原文

当我尝试从命令行执行 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.

为什么它认为这是一个类?你不能在一个长字符串中指定你的参数吗?

When I try to execute a jar from command line it works fine. I am trying to execute that same jar in another java program like so:

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()

However, this is giving me a 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.

Why does it think that is a class? Can you not specify your arguments in one long string?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

楠木可依 2024-10-17 05:36:18

对我来说很有意义。

也许您不打算运行一个名为“-Xmx1024m -Xms1024m -DTOOLS_DIR="/home/IM/work/dist”的类 -Daoi=whole -jar "/home/IM/work/dist/idt_tools.jar" 也许 是这些是单独的论点?

你的意思

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

Makes sense to me.

Perhaps you didn't intend to run a class called "-Xmx1024m -Xms1024m -DTOOLS_DIR="/home/IM/work/dist" -Daoi=whole -jar "/home/IM/work/dist/idt_tools.jar" perhaps you meant those to be seperate arguments?

try

ProcessBuilder pb = new ProcessBuilder("java", "-Xmx1024m", "-Xms1024m",
    "-DTOOLS_DIR=/home/IM/work/dist", "-Daoi=whole", 
    "-jar", "/home/IM/work/dist/idt_tools.jar");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文