By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,800 Members | 1,404 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,800 IT Pros & Developers. It's quick & easy.

How to access Javascript file from Java Code

100+
P: 144
Hi friend,

I have a below "keywsh.js" javascript file

Expand|Select|Wrap|Line Numbers
  1. var myApp = new ActiveXObject("Excel.Application"); 
  2. myApp.visible = false; 
  3. var shell = new ActiveXObject("WScript.shell"); 
  4. shell.run('"C:\\ROME Literature Spreadsheet.xls"',1); 
  5. WScript.sleep(5000); 
  6. shell.SendKeys("y"); 
  7. myApp.ActiveWorkbook.SaveCopyAs("C:\\goon\\today.xls");  
  8. shell.SendKeys("%{F4}"); 
  9.  
I want to access the above file "keywsh.js" from Java code. I am using the below java code to access "keywsh.js".when I tried to run this from below Java code. I am getting Error.


Expand|Select|Wrap|Line Numbers
  1. import java.io.FileReader; 
  2. import javax.script.ScriptEngine; 
  3. import javax.script.ScriptEngineManager; 
  4. import javax.script.Bindings; 
  5. import javax.script.ScriptContext; 
  6. import javax.script.ScriptException; 
  7.  
  8.  
  9. public class script1 { 
  10.   public static void main(String[] args) { 
  11.     ScriptEngineManager manager = new ScriptEngineManager(); 
  12.     ScriptEngine engine = manager.getEngineByName("js"); 
  13.     try { 
  14.       FileReader reader = new FileReader("C:\\keywsh.js"); 
  15.       engine.eval(reader); 
  16.       reader.close(); 
  17.     } catch (Exception e) { 
  18.       e.printStackTrace(); 
  19.     } 
  20.   } 
  21.  
Error Description:-

javax.script.ScriptException: sun.org.mozilla.javascript.internal.EcmaError: Ref
erenceError: "ActiveXObject" is not defined. (<Unknown source>#4) in <Unknown so
urce> at line number 4
at com.sun.script.javascript.RhinoScriptEngine.eval(R hinoScriptEngine.ja
va:110)
at javax.script.AbstractScriptEngine.eval(AbstractScr iptEngine.java:232)

at script1.main(script1.java:18)


Can you help me to run this javacode successfully? Is there is any other Java code? Pl help me.
Mar 11 '12 #1
Share this Question
Share on Google+
1 Reply


10K+
P: 13,264
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.
Mar 12 '12 #2

Post your reply

Sign in to post your reply or Sign up for a free account.