4

Possible Duplicate:
Execute another jar in a java program

I have an executable jar file that accepts two parameters: an input file and an output file:

java -jar inputFile.txt outputFile.txt

How can I call exactly this from JavaScript?

Community
  • 1
  • 1
Veni_Vidi_Vici
  • 291
  • 1
  • 6
  • 16
  • 5
    I'm hoping you understand the complete and utter difference between Java and JavaScript. Are you building a website or web application? – Jon Egeland Aug 06 '12 at 04:00
  • I've edited the question so that it makes some sense. If you don't find the edits appropriate, please roll them back. – Blender Aug 06 '12 at 04:08
  • 1
    http://stackoverflow.com/questions/1320476/execute-another-jar-in-a-java-program – James Aug 06 '12 at 04:09
  • @Blender: That changes the meaning of the question. The original was about how to call it from JavaScript, not Java, which is a perfectly reasonable (if uncommon) thing to do. – Mechanical snail Aug 06 '12 at 04:40

2 Answers2

6

The answer depends on the context.

  • If your javascript is running in a web browser sandbox, then the answer is that you can't run a JAR file. The sandbox is designed to stop you doing that kind of thing.

  • If your javascript is running under node.js, then this SO Q&A offers a solution to the problem of running a command: How do I run the system commands in javascript?. This can be used to run the java command with the appropriate args.

  • If your javascript is trusted code running in a browser with a Java plugin, then you may be able to make a call to java.lang.System.exec(...) passing an appropriate java command line in the appropriate fashion. You may also be able to create a classloader, read the JAR file's manifest, extract the entry point class, load it, and call the "main" method.

Community
  • 1
  • 1
Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
-5
java -jar executable.jar inputFile.txt outputFile.txt

For further details see the java command synopsis:

SYNOPSIS

  • java [ options ] class [ argument ... ]
  • java [ options ] -jar file.jar [ argument ... ]
  • javaw [ options ] class [ argument ... ]
  • javaw [ options ] -jar file.jar [ argument ... ]
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433