Replace <Unknown Source> in EcmaError (JSR223) with actual file name
Hello everyone,
In my code, I have a bunch of scripts contained in .js files. Whenever one of the scripts contains an error, I get this:
javax.script.ScriptException: sun.org.mozilla.javascript.internal.EcmaError: ReferenceError: "nonexistant" is not defined. (<Unknown source>#5) in <Unknown source> at line number 5
What bugs me is the <Unknown Source>. Multiple files are in one ScriptContext, and it can be hard to track down an error. It also looks horrible.
Is there a way to replace <Unknown Source> with the actual file name? None of the methods I see support passing a File object, so I'm really confused here.
Mirrored at Replace <Unknown Source> in Java Rhino (JSR223) with actual file name - Stack Overflow
Re: Replace <Unknown Source> in EcmaError (JSR223) with actual file name
The file name, line number, and variable name info are included in the class files when compiled with the debug option -g. The JVM exception management can then pick them up from the class files at runtime to provide more informative messages. See Java Compiler Options.
Re: Replace <Unknown Source> in EcmaError (JSR223) with actual file name
Quote:
Originally Posted by
dlorde
The file name, line number, and variable name info are included in the class files when compiled with the debug option -g. The JVM exception management can then pick them up from the class files at runtime to provide more informative messages. See
Java Compiler Options.
You must not be familiar with ScriptEngine. The files aren't compiled, their plain JavaScript files that are dynamically loaded and parsed. And that's not a compile error, thats a runtime error.
Re: Replace <Unknown Source> in EcmaError (JSR223) with actual file name
Quote:
Originally Posted by
Lord.Quackstar
You must not be familiar with ScriptEngine.
You're right, I'm not (well, not that familiar)...
Quote:
The files aren't compiled, their plain JavaScript files that are dynamically loaded and parsed. And that's not a compile error, thats a runtime error.
:confused: I wasn't suggesting it was a compiler error, I was just saying that in order to get the filename and line number of Java source in exception messages, the javac -g debug options should be used when compiling Java source code. Clearly this will only tell you the last Java class file and line executed, not JavaScript innards, but I thought this was a Java question. If it doesn't apply here, then I'm sorry, it won't help :((
Re: Replace <Unknown Source> in EcmaError (JSR223) with actual file name
Any chance you can run your Javascript code in a normal browser by including it from a dummy HTML file, that way you could use the browsers Javascript debugger which is probably a lot better than your Java IDE :D
// Json
Re: Replace <Unknown Source> in EcmaError (JSR223) with actual file name
Quote:
Originally Posted by
Json
Any chance you can run your Javascript code in a normal browser by including it from a dummy HTML file, that way you could use the browsers Javascript debugger which is probably a lot better than your Java IDE :D
// Json
That would be probably the best way, but the issue though is that I also load util functions and place then at the top of the file (hence why its hard when an error like that shows up because I have no idea if its the util files or the command file).
And also this is java scripting, not browser scripting. Surly there's support for this since EcmaError has a spot for the file name.
Re: Replace <Unknown Source> in EcmaError (JSR223) with actual file name
Yes, point taken, I also think it should give you a nicer message than Unknown Source, is there anyway through the ScriptEngine you can set a debug flag of some kind?
I'm not too familiar with the ScriptEngine myself you see.
// Json