Either make a jar file with the Java class in Listing 1 in it and name the jar file javascript-runner.jar, or download javascript-runner.jar from https://github.com/ted-gao/teds-code/downloads. Place javascript-runner.jar somewhere in your local file system, for example, C:\toolkit.
Listing 1
package coder.toolkit; import java.io.FileReader; import javax.script.ScriptEngine; import javax.script.ScriptEngineManager; public class CoreJavaScriptRunner { /** * @param args - a single program argument is expected. It should be a full path * to the core JavaScript to run. */ public static void main(String[] args) { ScriptEngineManager manager = new ScriptEngineManager(); ScriptEngine engine = manager.getEngineByName("js"); try { FileReader reader = new FileReader(args[0]); engine.eval(reader); reader.close(); } catch (Exception e) { e.printStackTrace(); } } }
From Eclipse Workbench, create an External Tools Configuration following the steps below:
- Click the menu item Run | External Tools | External Tools Configurations ...
- On the pop-up External Tools Configurations dialog, right click Program, then click New, as in Figure 1.
- Fill in the right pane of the External Tools Configurations dialog, as in Figure 2.
- Click the Apply button, and then the Close button to close the External Tools Configurations dialog.
Figure 1 - Create a New External Tool Configuration |
Figure 2 - The Core JavaScript Runner |
(Assume that you have already installed JavaScript Development Tools in Eclipse) Now You can create a JavaScript project and a JavaScript file in it. Open the JavaScript file with the JavaScript Editor, (if wanted, edit the script,) then click the External Tool button and select Core JavaScript Runner, as in Figure 3 below. Eclipse will run the selected JavaScript. And the output will be written to the Console.
Figure 3 - Run Core JavaScript |
Figure 4 - An Example |
Appendix - Create JavaScript Project in Eclipse
To create a JavaScript project in Eclipse, from the Workbench, follow the steps below:
- Click menu item File | New | Project ...
- On the pop-up New Project dialog appears, select JavaScript | JavaScript Project, and click the Next button.
- Fill in a project name, and click the Finish button.
2 comments:
Perfect solution, thanks !
Nice article.
This can be streamlined, instead of using external tools, you can add Java project facets to the project and then go into Run Configurations, create an entry into Java Application and put the CoreJavaScriptRunner in there. Add the ${selected_resource_loc} as a program argument and then you are golden.
Post a Comment