How to access Javascript file from Java Code

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • davidson1
    New Member
    • Feb 2008
    • 144

    How to access Javascript file from Java Code

    Hi friend,

    I have a below "keywsh.js" javascript file

    Code:
    var myApp = new ActiveXObject("Excel.Application"); 
    myApp.visible = false; 
    var shell = new ActiveXObject("WScript.shell"); 
    shell.run('"C:\\ROME Literature Spreadsheet.xls"',1); 
    WScript.sleep(5000); 
    shell.SendKeys("y"); 
    myApp.ActiveWorkbook.SaveCopyAs("C:\\goon\\today.xls");  
    shell.SendKeys("%{F4}");
    I want to access the above file "keywsh.js" from Java code. I am using the below java code to access "keywsh.js".whe n I tried to run this from below Java code. I am getting Error.


    Code:
    import java.io.FileReader; 
    import javax.script.ScriptEngine; 
    import javax.script.ScriptEngineManager; 
    import javax.script.Bindings; 
    import javax.script.ScriptContext; 
    import javax.script.ScriptException; 
      
      
    public class script1 { 
      public static void main(String[] args) { 
        ScriptEngineManager manager = new ScriptEngineManager(); 
        ScriptEngine engine = manager.getEngineByName("js"); 
        try { 
          FileReader reader = new FileReader("C:\\keywsh.js"); 
          engine.eval(reader); 
          reader.close(); 
        } catch (Exception e) { 
          e.printStackTrace(); 
        } 
      } 
    }
    Error Description:-

    javax.script.Sc riptException: sun.org.mozilla .javascript.int ernal.EcmaError : Ref
    erenceError: "ActiveXObj ect" is not defined. (<Unknown source>#4) in <Unknown so
    urce> at line number 4
    at com.sun.script. javascript.Rhin oScriptEngine.e val(R hinoScriptEngin e.ja
    va:110)
    at javax.script.Ab stractScriptEng ine.eval(Abstra ctScr iptEngine.java: 232)

    at script1.main(sc ript1.java:18)


    Can you help me to run this javacode successfully? Is there is any other Java code? Pl help me.
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    The Javascript engine you are using doesn't know the ActiveXObject. Either find the engine that knows that object if it's available and allowed to load it or use java to do the task without calling and Javascript. You don't need to call a Javascript function to open an excel workbook.

    Comment

    Working...