2014-11-05 29 views
1

我使用eclipse luna作为IDE。我正在从事Web应用程序。 当我在JavaScript中编码时,有时我忘记放分号,但如果我的JavaScript在浏览器中运行,所有东西仍然正常工作。Nashorn - 调试JavaScript运行在Nashorn

但是当我在犀牛运行我的JavaScript,它会抛出一个错误,因为我并没有把分号

错误们的说法

nashorn Error: :1:68 Expected ; but found var ..... ... ... in at line number 1 at column number 68

的问题是,控制台总是显示我第1行的错误,我认为是因为nashorn读取我的JavaScript文件为单行,但如实地说,我的JavaScript文件包含很多行。

这是很难找到的错误,因为控制台总是说错误是行号1

我知道什么是错我的代码,但我不知道如何解决它。

Nashorn.java

public class Nashorn { 

    public Nashorn() { 
    } 

    public static String readFileAsText(String theUrl){ 
     String allLine = ""; 
     try { 
      URL url = new URL(theUrl); 
      BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream())); 

      String inputLine = ""; 
      while ((inputLine = in.readLine()) != null){ 
       allLine += inputLine; 
       //System.out.println("read : "+inputLine); 
      } 
      in.close(); 
     } catch (MalformedURLException e) { 
      System.out.println("url Error: "); 
     } catch (IOException e) { 
      System.out.println("I/O Error: "); 
     } 
     return allLine; 
    } 

    public static void main(String[] args) { 
     ScriptEngineManager scriptEngineManager = new ScriptEngineManager(null); 
     ScriptEngine scriptEngine = scriptEngineManager.getEngineByName("nashorn"); 

     try { 
      //readFileAsText 
      String room = Nashorn.readFileAsText("http://localhost:8080/monsterpuzzle/data/Room.js"); 
      scriptEngine.eval(room); 
     } catch (Exception e) { 
      // TODO Auto-generated catch block 
      System.out.println("nashorn Error: "+e.getMessage()); 
      e.printStackTrace(); 
     } 
    } 
} 

Room.js

var Room = function(){ 
    this.test = function(){ 
     print("lalala"); 
    }; 
} 

var room = new Room(); 
room.test(); 

我的问题:

犀牛总是显示在我行号1,错误但是,该错误不在行号1中,而是在另一行中。我认为这是因为nashorn将我的整行代码读为单行。

我的问题:

如何解决这一问题?

(所以如果在第6行错误,控制台会说,在第6行误差不1号线),因为你正在上一行

+0

对不起,我从来没有用过犀牛,所以我只能尽量给你提示...通常,当我们看到在1号线这是因为JavaScript是精缩错误。也许在nashorn/eclipse中有一些选项可以启用自动缩小功能,并且此选项已打开? – Joel 2014-11-05 10:52:32

回答

3

JavaScript是最终成为一条线。

while ((inputLine = in.readLine()) != null){ 
    allLine += inputLine; 
    //System.out.println("read : "+inputLine); 
} 

试试这个:

while ((inputLine = in.readLine()) != null){ 
    allLine += inputLine + "\n"; /* ADD NEWLINE CHARACTER AT END OF LINE */ 
    //System.out.println("read : "+inputLine); 
} 
+0

长时间回应道歉..我认为没有人在这里..我已经尝试过你的代码之前,但仍然有错误..而是阅读所有的线..我最终与eval(“负载('”+ myFile +“' );“); – user3578021 2014-11-22 13:08:04

相关问题