user3578021
user3578021

Reputation: 103

Nashorn - debug javascript run in Nashorn

I use eclipse luna as IDE. I'm working in web application. When I code in javascript, sometimes I forget to put semicolon, but everything still work fine if my javascript run in browser.

But when I run my javascript in nashorn, it will throw an error because I didn't put semicolon

the error say s

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

The problem is, the console always shows me the error at line number 1, I think because nashorn reads my javascript file as single line, but truthfully my javascript file contains many lines.

It is hard to find the error because the console always say the error is in line number 1.

I know something is wrong with my code, but I don't know how to fix it.

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();

my problem :

Nashorn always shows me the error at line number 1. However, the error is not in line number 1 but in another line. I think it happens because nashorn reads my entire line of code as single line.

my question :

How to fix this?

(so if there error in line 6, the console will say error in line 6 not line 1)

Upvotes: 1

Views: 2241

Answers (1)

David P. Caldwell
David P. Caldwell

Reputation: 3821

Your JavaScript is ending up as one line because you are making it one line.

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

Try this:

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

Upvotes: 3

Related Questions