好吧,我正在尝试从我的项目中的Xbootclasspath一个jar。目前,我必须通过命令行使用以下命令加载我的应用程序:
java -Xbootclasspath/p:canvas.jar -jar application.jar
这完全正常,但我想在不必输入命令行的情况下这样做,是否我可以在jar中使用Xbootclasspath?
感谢。
答案 0 :(得分:2)
最明确的解决方案是拥有两个主要类。
您的第一个名为Boot
或类似的类将成为应用程序的外部入口点,如jar的清单中所设置的那样。该类将形成必要的运行时命令,以使用Xboot参数启动实际的主类(名为Application
或类似)。
public class Boot {
public static void main(String[] args) {
String location = Boot.class.getProtectionDomain().getCodeSource().getLocation().getPath();
location = URLDecoder.decode(location, "UTF-8").replaceAll("\\\\", "/");
String app = Application.class.getCanonicalName();
String flags = "-Xbootclasspath/p:canvas.jar";
boolean windows = System.getProperty("os.name").contains("Win");
StringBuilder command = new StringBuilder(64);
if (windows) {
command.append("javaw");
} else {
command.append("java");
}
command.append(' ').append(flags).append(' ');
command.append('"').append(location).append('"');
// append any necessary external libraries here
for (String arg : args) {
command.append(' ').append('"').append(arg).append('"');
}
Process application = null;
Runtime runtime = Runtime.getRuntime();
if (windows) {
application = runtime.exec(command.toString());
} else {
application = runtime.exec(new String[]{ "/bin/sh", "-c", command.toString() });
}
// wire command line output to Boot to output it correctly
BufferedReader strerr = new BufferedReader(new InputStreamReader(application.getErrorStream()));
BufferedReader strin = new BufferedReader(new InputStreamReader(application.getInputStream()));
while (isRunning(application)) {
String err = null;
while ((err = strerr.readLine()) != null) {
System.err.println(err);
}
String in = null;
while ((in = strin.readLine()) != null) {
System.out.println(in);
}
try {
Thread.sleep(50);
} catch (InterruptedException ignored) {
}
}
}
private static boolean isRunning(Process process) {
try {
process.exitValue();
} catch (IllegalThreadStateException e) {
return true;
}
return false;
}
}
您的Application
课程会运行您的实际课程:
public class Application {
public static void main(String[] args) {
// display user-interface, etc
}
}
答案 1 :(得分:0)
感觉很难吃,但是你能用一个Runtime.exec来调用带有提供的选项的java和一个新参数(以及一些寻找它的条件代码)来阻止产生新进程的递归循环吗?