java执行js文件

 
    1. function browse() {
    2.     var guiPkgs = new JavaImporter(java.awt, java.awt.event,
    3.                              javax.swing, javax.swing.undo,
    4.                              javax.swing.event, javax.swing.text);
    5.     with (guiPkgs) {
    6.         var desktop = null;
    7.         if (Desktop.isDesktopSupported()) {
    8.             desktop = Desktop.getDesktop();
    9.         } else {
    10.             alert("no desktop support");
    11.             return;
    12.         }
    13.         if (desktop.isSupported(Desktop.Action.BROWSE)) {
    14.             desktop.browse(new java.net.URI("http://blog.csdn.net/dashi99"));
    15.         } else {
    16.             alert("no browser support");
    17.         }
    18.     }
    19. }
    20. if (this.application != undefined) {
    21.     this.application.addTool("Browse", browse);
    22. }
    import java.util.*;
  1. import java.io.*;
  2. import javax.script.*;
  3. public class Main
  4. {
  5.     public Main()
  6.     {
  7.         try {
  8.             ScriptEngineManager m = new ScriptEngineManager();
  9.             ScriptEngine engine = m.getEngineByName("javascript");
  10.             if ( engine != null )
  11.             {
  12.                 InputStream is = this.getClass().getResourceAsStream("browse.js");
  13.                 Reader reader = new InputStreamReader(is);
  14.                 engine.eval(reader);
  15.                 Invocable invocableEngine = (Invocable)engine;
  16.                 invocableEngine.invokeFunction("browse");
  17.             }
  18.         }
  19.         catch ( Exception e ) {
  20.             e.printStackTrace();
  21.         }
  22.     }
  23.     
  24.     public static void main(String[] args)
  25.     {
  26.         Main m = new Main();
  27.     }
  28. }

 

 

 

你可能感兴趣的:(Java,SE,java,javascript,string,null)