设置类路径 java 以在 Runtime.exec 中使用
我正在尝试使用 Runtime.exec 生成一个进程。我想使用当前的类路径: System.getProperty("java.class.path")
不幸的是,我遇到了各种各样的问题。当它在我的 Mac 上运行时,它在 Windows 上不起作用。当类路径中有空格时,在我的 Mac 上就无法工作。我总是得到的错误是 ClassDefNotFound,所以它与我如何构建和传递类路径有关。
这是一些示例代码:
String startClass = "com.test.MyClass"
String javaHome = System.getProperty("java.home");
String javaCmd = javaHome + "/bin/java";
String classPath = "-Djava.class.path=" + System.getProperty("java.class.path");
String[] commands = new String[]{javaCmd, classPath, startClass};
String commandString = StringUtils.join(commands, " ");
Process process = Runtime.getRuntime().exec(commandString);
那么,我应该如何设置类路径?
感谢您的帮助
I am trying to spawn a process using Runtime.exec. I want to use my current classpath : System.getProperty("java.class.path")
Unfortunately, I am having all kinds of issues. When it works on my mac, it doesn't work on Windows. And doesn't work on my mac ever when there is a space in the classpath. The error I always get is ClassDefNotFound, so it's related to how I'm building and passing in the classpath.
here is some sample code:
String startClass = "com.test.MyClass"
String javaHome = System.getProperty("java.home");
String javaCmd = javaHome + "/bin/java";
String classPath = "-Djava.class.path=" + System.getProperty("java.class.path");
String[] commands = new String[]{javaCmd, classPath, startClass};
String commandString = StringUtils.join(commands, " ");
Process process = Runtime.getRuntime().exec(commandString);
So, how should I setup the classpath?
Thanks for any help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要瞄准采用
String[]
而不是 String 的 'exec' 重载。并且您已使用File
类中的正确路径分隔符,以便在 Linux 上使用冒号,在 Windows 上使用分号。You need to aim for the overload of 'exec' that takes
String[]
, not String. And you have use the correct path separator from theFile
class, so that you have colons on Linux and semicolons on Windows.