Nashorn的FileNotFoundException

时间:2014-06-22 21:50:22

标签: java java-ee servlets

我正在和nashorn玩一点,我做了一个表单和一个servlet,但是我无法获得脚本文件。 我有这个代码。对servlet的调用运行良好,以及hello world消息。

engine.eval(new FileReader("C:\\Users\\Gabriel\\Documents\\NetBeansProjects\\WebApplication1\\src\\java\\paquete\\script.js"));

鉴于绝对路径完美无缺......但我想给出相对路径

    HttpSession sesion=request.getSession();
    String submit=request.getParameter("submit");
            ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn");
        try {
            System.out.println("PATH:"+request.getContextPath());
            engine.eval(new FileReader("src/java/paquete/script.js"));
        } catch (ScriptException ex) {
            Logger.getLogger(Servlet.class.getName()).log(Level.SEVERE, null, ex);
        }

            Invocable invocable = (Invocable) engine;

    switch(submit){
    case "Login":
            try {
                engine.eval("print('Hello World!');");
            } catch (ScriptException ex) {
                Logger.getLogger(Servlet.class.getName()).log(Level.SEVERE, null, ex);
            }
                break;
}

我的脚本文件位于servlet所在的位置。

给我这个痕迹

 java.io.FileNotFoundException: src\java\paquete\script.js (El sistema no puede encontrar la ruta especificada)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:131)
at java.io.FileInputStream.<init>(FileInputStream.java:87)
at java.io.FileReader.<init>(FileReader.java:58)
at paquete.Servlet.doPost(Servlet.java:67)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:707)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:318)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:357)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:260)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:188)
at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:191)
at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:168)
at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:189)
at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:288)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:206)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:136)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:114)
at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:838)
at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:113)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:115)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:55)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:135)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:564)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:544)
at java.lang.Thread.run(Thread.java:745)

感谢您的帮助

1 个答案:

答案 0 :(得分:0)

之前我遇到过类似的问题,我解决它的方法是评估一个&#34; load&#34; javascript功能。 所以,而不是做

engine.eval(new FileReader("C:\\Users\\Gabriel\\Documents\\NetBeansProjects\\WebApplication1\\src\\java\\paquete\\script.js"));

你可以尝试做

engine.eval("load('src/java/paquette/script.js')");

并在之后使用Invocable引擎。

希望它有所帮助!