2013-05-01 18 views
0

這是我原來的職位的延續在這裏:javascript-to-java-applet-using-deployjava-js-to-run-commandlineJavaScript以Java小程序 - 工作

我非常新到Java。我想創建一個Java Applet,它將允許我的JavaScript將命令行傳遞給Java Applet。這隻會在我的開發機器上運行 - 不需要提醒我什麼是安全問題。用例是我爲ExtJS應用程序設計了一個introspector,它允許我顯示這些類。我希望能夠點擊一個類,將相關路徑名傳遞給Applet,並在Eclipse中打開該文件進行編輯。

經過多次失敗的測試,這是我發現的作品。感謝Andrew Thompson和下面提到的其他人。

似乎有兩條路徑,我設法讓他們都工作。我把他們都包括在這裏。路徑1是用參數執行一個程序(例如D:/Eclipse/eclipse.exe --launcher.openFile C:/sites/test.js),而路徑2則是設置Win7以在打開* .js文件(即將* .js與Eclipse關聯)時打開Eclipse。

安全免責聲明:請記住,路徑1在公共服務器上完全不安全 - 通過JavaScript傳遞格式或刪除命令或任何其他惡作劇相對容易!

對於像我這樣的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,我出口這兩個類位於同一文件夾作爲一個jar文件名爲runco​​mbo.jar跟隨HTML文件。然後我自己也簽署了安全問題所需的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> 

注意,小程序被添加到DOM當JS函數被調用。這是使用Java安全性的一部分,並避免了上述阻止applet運行的安全問題。還要注意有兩個JS函數調用來匹配不同的類。

再次感謝所有幫助我實現這一目標的人。現在我可以回到最初的目的 - 完成我的Ext Introspector!

穆雷

+0

請注意,答案不應該成爲問題的一部分。你可能會重寫這個問題,並將其餘大部分放在答案中。 – 2013-05-01 06:38:20

回答

0

你不需要做小程序的每一次一個新的實例,使用MAYSCRIPT標籤的小程序可以讓瀏覽器允許Java腳本通信應用程序。

你也應該清理init方法

<head> 
    <script type="text/javascript"> 
    //whatToDo can be from an input box 
    //in applet check if its del or rmdir or move or delete at a minimum 
     function callJavaCmd (whatToDo) { 
       // call the MessageBox method of the applet 
      var applet = document.getElementById ("myApplet"); 
      applet.command (whatToDo); 
     } 

    </script> 
</head> 
<body> 
    <applet id="myApplet" code="Your.class" codebase="/base/" 
      mayscript="mayscript" style="width:300px; height:50px;"> 
    </applet> 

參考http://help.dottoro.com/lhbkaqko.phphttp://docs.oracle.com/javase/6/docs/technotes/guides/plugin/developer_guide/java_js.html舊的Netscape類是每個JVM(netscape.javascript的一部分。)