我在myeclipse下找不到javax.script.*,请大虾帮忙!!!

本文介绍了一个使用Java运行JavaScript文件的示例程序,并详细展示了如何通过命令行参数定义变量及指定要执行的脚本文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

具体类

package other;

import javax.script.*;
import java.io.*;

// Evaluate a file of JavaScript and print its result
public class RunScript {
    public static void main(String[] args) throws IOException {
        // Obtain an interpreter or "ScriptEngine" to run the script.
        ScriptEngineManager scriptManager = new ScriptEngineManager( );
        ScriptEngine js = scriptManager.getEngineByExtension("js");

        // The script file we are going to run
        String filename = null;

        // A Bindings object is a symbol table for or namespace for the
        // script engine. It associates names and values and makes
        // them available to the script.
        Bindings bindings = js.createBindings( );

        // Process the arguments. They may include any number of
        // -Dname=value arguments, which define variables for the script.
        // Any argument that does not begin with -D is taken as a filename
        for(int i = 0; i < args.length; i++) {
            String arg = args[i];
            if (arg.startsWith("-D")) {
                int pos = arg.indexOf('=');
                if (pos == -1) usage( );
                String name = arg.substring(2, pos);
                String value = arg.substring(pos+1);
                // Note that all the variables we define are strings.
                // Scripts can convert them to other types if necessary.
                // We could also pass a java.lang.Number, a java.lang.Boolean
                // or any Java object or null.
                bindings.put(name, value);
            }
            else {
                if (filename != null) usage( ); // only one file please
                filename = arg;
            }
        }
        // Make sure we got a file out of the arguments.
        if (filename == null) usage( );

        // Add one more binding using a special reserved variable name
        // to tell the script engine the name of the file it will be executing.
        // This allows it to provide better error messages.
        bindings.put(ScriptEngine.FILENAME, filename);

        // Get a stream to read the script.
        Reader in = new FileReader(filename);

        try {
            // Evaluate the script using the bindings and get its result.
            Object result = js.eval(in, bindings);
            // Display the result.
            System.out.println(result);
        }
        catch(ScriptException ex) {
            // Or display an error message.
            System.out.println(ex);
        }
    }

    static void usage( ) {
        System.err.println(
                 "Usage: java RunScript [-Dname=value...] script.js");
        System.exit(1);
    }
}


报错:

Exception in thread "main" java.lang.Error: Unresolved compilation problems:
 ScriptEngineManager cannot be resolved to a type
 ScriptEngineManager cannot be resolved to a type
 ScriptEngine cannot be resolved to a type
 Bindings cannot be resolved to a type
 ScriptEngine cannot be resolved
 ScriptException cannot be resolved to a type
 ex cannot be resolved

 at other.RunScript.main(RunScript.java:10)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值