よし、プロジェクト内から jar を Xbootclasspath しようとしています。現在、次のコマンドを使用して、コマンドラインからアプリケーションをロードする必要があります。
java -Xbootclasspath/p:canvas.jar -jar application.jar
これは完全に正常に動作しますが、コマンドラインを入力せずにこれを実行したいのですが、jar 内から Xbootclasspath を実行する方法はありますか?
ありがとう。
よし、プロジェクト内から jar を Xbootclasspath しようとしています。現在、次のコマンドを使用して、コマンドラインからアプリケーションをロードする必要があります。
java -Xbootclasspath/p:canvas.jar -jar application.jar
これは完全に正常に動作しますが、コマンドラインを入力せずにこれを実行したいのですが、jar 内から Xbootclasspath を実行する方法はありますか?
ありがとう。
最も明確な解決策は、2 つの主要なクラスを持つことです。
jar のマニフェストで設定されているように、最初のクラス (名前付きBoot
または類似) は、アプリケーションへの外部エントリ ポイントになります。Application
このクラスは、Xboot パラメーターを使用して、実際のメイン クラス (名前付きまたは同様のもの) を開始するために必要なランタイム コマンドを形成します。
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
}
}