简体   繁体   English

设置要在Runtime.exec中使用的类路径Java

[英]Setting classpath java for use in Runtime.exec

I am trying to spawn a process using Runtime.exec. 我正在尝试使用Runtime.exec生成一个进程。 I want to use my current classpath : System.getProperty("java.class.path") 我想使用当前的类路径: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. 当它在我的Mac上运行时,在Windows上不运行。 And doesn't work on my mac ever when there is a space in the classpath. 当类路径中有空格时,在我的Mac上也无法使用。 The error I always get is ClassDefNotFound, so it's related to how I'm building and passing in the classpath. 我总是得到的错误是ClassDefNotFound,所以它与我如何构建和传递类路径有关。

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 谢谢你的帮助

You need to aim for the overload of 'exec' that takes String[] , not String. 您需要针对采用String[]而不是String的'exec'的重载。 And you have use the correct path separator from the File class, so that you have colons on Linux and semicolons on Windows. 并且您使用了File类中的正确路径分隔符,因此在Linux上使用冒号,在Windows上使用分号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM