これは、私の元の投稿の続きです: javascript-to-Java-applet-using-deployjava-js-to-run-commandline
私はJavaにかなり慣れていません。JavaScript がコマンドラインを Java アプレットに渡すことができる Java アプレットを作成したいと考えています。これは私の開発マシンでのみ実行されます - セキュリティの問題が何であるかを思い出させる必要はありません。ユースケースは、クラスを表示できる ExtJS アプリのイントロスペクターがあることです。クラスをクリックして、関連するパス名をアプレットに渡し、そのファイルを Eclipse で開いて編集できるようにしたいと考えています。
多くの失敗したテストの後、これが機能することがわかりました。アンドリュー・トンプソンおよび以下に挙げるその他の方々に感謝します。
2つのパスがあるようで、両方を機能させることができました。ここに両方を含めます。パス 1 はパラメーター (例: D:/Eclipse/eclipse.exe --launcher.openFile C:/sites/test.js
) を指定してプログラムを実行することで、パス 2 は *.js ファイルが開かれたときに Eclipse を開くように Win7 を設定することです (つまり、*.js を Eclipse に関連付けます)。
セキュリティに関する免責事項: パス 1 は公開サーバーでは完全に安全ではないことに注意してください。JavaScript を介して format コマンドや delete コマンドを渡すのは比較的簡単です!!
私のような Java の初心者のために、手順についてできる限り明確に説明します。
プログラムを実行するためのクラス。ありがとうございます: https://stackoverflow.com/users/80389/corgrath
package systemcmd;
import java.applet.Applet;
import java.io.*;
public class Runcmd extends Applet {
private static final long serialVersionUID = 1L;
public void init() {
// It seems that even though this command is not executed when the
// applet is run via JS we still need to refer to the exec command here,
// I presume so it is linked? If I comment these out altogether, the
// applet doesnt work. Either of the following will suffice.
// FYI. Just betraying my Java newbie status! :-)
//exec("notepad c:/sites/test.txt");
exec("calc");
}
// From https://stackoverflow.com/questions/1068271/signed-applet-gives-accesscontrolexception-access-denied-when-calling-from-jav
// Thank you !!!
public void exec(String command) {
try {
// launch EXE and grab stdin/stdout and stderr
Process process = Runtime.getRuntime().exec(getParameter("command"));
// OutputStream stdin = process.getOutputStream();
InputStream stderr = process.getErrorStream();
InputStream stdout = process.getInputStream();
// clean up if any output in stdout
String line = "";
BufferedReader brCleanUp = new BufferedReader(new InputStreamReader(stdout));
while ((line = brCleanUp.readLine()) != null) {
//System.out.println ("[Stdout] " + line);
}
brCleanUp.close();
// clean up if any output in stderr
brCleanUp = new BufferedReader(new InputStreamReader(stderr));
while ((line = brCleanUp.readLine()) != null) {
//System.out.println ("[Stderr] " + line);
}
brCleanUp.close();
} catch (Exception exception) {
exception.printStackTrace();
}
}
}
ファイルに関連付けられたプログラムを実行するためのクラス:
package systemcmd;
import java.applet.Applet;
import java.awt.Desktop;
import java.io.*;
public class Launchapp extends Applet {
private static final long serialVersionUID = 1L;
public void init() {
// It seems that even though this command is not executed when the
// applet is run via JS we still need to refer to the exec command here,
// I presume so it is linked? If I comment these out altogether, the
// applet doesnt work. Either of the following will suffice.
// FYI. Just betraying my Java newbie status! :-)
//launch("notepad c:/sites/test.txt");
launch("calc");
}
// From https://stackoverflow.com/questions/1068271/signed-applet-gives-accesscontrolexception-access-denied-when-calling-from-jav
// Thank you !!!
public void launch(String command) {
try {
Desktop.getDesktop().open(new File(getParameter("command")));
} catch (Exception exception) {
exception.printStackTrace();
}
}
}
Eclipse を使用して、これら 2 つのクラスを、次の HTML ファイルと同じフォルダーにある runcombo.jar という 1 つの jar ファイルにエクスポートしました。次に、セキュリティの問題にも必要なjarに自己署名しました。このチュートリアルは、そのプロセスに役立つことがわかりました。http://www.jade-cheng.com/uh/ta/signed-applet-tutorial/ .
HTML と JavaScript のテスト ページ:
<html>
<head>
<script type="text/javascript">
function exec( command ) {
var applet = "<object type='application/x-java-applet' height='100' width='100' name='jsApplet'><param name='code' value='systemcmd.Runcmd'/><param name='archive' value='runcombo.jar' /><param name='mayscript' value='true'/><param name='command' value='" + command + "'/>Applet failed to run. No Java plug-in was found.</object>";
var body = document.getElementsByTagName("body")[0];
var div = document.createElement("div");
div.innerHTML = applet;
body.appendChild(div);
}
function launch( command ) {
var applet = "<object type='application/x-java-applet' height='100' width='100' name='jsApplet'><param name='code' value='systemcmd.Launchapp'/><param name='archive' value='runcombo.jar' /><param name='mayscript' value='true'/><param name='command' value='" + command + "'/>Applet failed to run. No Java plug-in was found.</object>";
var body = document.getElementsByTagName("body")[0];
var div = document.createElement("div");
div.innerHTML = applet;
body.appendChild(div);
}
</script>
</head>
<body>
<a href="#" onclick="exec('notepad c:/sites/test.txt');">Exec Notepad</a>
<br> <a href="#" onclick="exec('calc');">Exec Calculator</a>
<br> <a href="#" onclick="exec('D:/Eclipse/eclipse.exe --launcher.openFile C:/sites/test.js');">Exec Eclipse with command line parms</a>
<br> <a href="#" onclick="launch('C:/sites/test2.txt');">Launch Notepad via *.txt association</a>
<br> <a href="#" onclick="launch('C:/sites/test2.js');">Launch Eclipse via *.js association</a>
</body>
</html>
JS 関数が呼び出されると、アプレットが DOM に追加されることに注意してください。これは Java セキュリティの一部であり、アプレットの実行を停止させた前述のセキュリティの問題を回避します。また、異なるクラスに一致する 2 つの JS 関数呼び出しがあることにも注意してください。
これを機能させるのを手伝ってくれたすべての人にもう一度感謝します。Ext Introspector を完成させるという本来の目的に戻ることができます。
マレー