Eclipse中如何运行Javascript

这篇文章很有帮助,原文链接如下,担心文章被移除,Mark一下:

http://ted-gao.blogspot.com/2012/06/running-core-javascript-in-eclipse.html


With the Eclipse JavaScript Development Tools, one can create JavaScript projects, and create and edit JavaScript files. The JavaScript editor provides JavaScript syntax highlight and formatting. In this regard, the JavaScript Development Tools are pretty much like the Java Development Tools. However, unlike in the Java Development Tools, with which one can run the Java class selected in the Java Editor, one cannot simply run the JavaScript file selected in the JavaScript Editor. This article teaches, with a little "trick", one will be able to do so - almost.

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 fromhttps://github.com/ted-gao/teds-code/downloads. Place javascript-runner.jar somewhere in your local file system, for example, C:\toolkit.

Listing 1

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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:
  1. Click the menu item Run | External Tools | External Tools Configurations ...
  2. On the pop-up External Tools Configurations dialog, right click Program, then click New, as in Figure 1.
  3. Fill in the right pane of the External Tools Configurations dialog, as in Figure 2.
  4. 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:
  1. Click menu item File | New | Project ...
  2. On the pop-up New Project dialog appears, select JavaScript | JavaScript Project, and click the Next button.
  3. Fill in a project name, and click the Finish button.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值