问题
In my java program I make heavy use of Suns implmentation of the Rhino script engine. Very recently however, my JDK does not seem to automatically import the rt.jar file anymore when compiling.
Whats strange is that NetBeans reports 0 live errors, they only show up when doing a complete Clean & Build. This wasn't happening before when I was importing NativeArray, so I'm really confused on why it all of a sudden stopped working.
Specs:
- OS - Windows
- Java version - java version "1.6.0_20"
- Javac version - javac 1.6.0_20
- NetBeans version - 6.9
Check to see if it exists:
C:\Documents and Settings\LordQuackstar\Desktop\TestApp\src>javap sun.org.mozill a.javascript.internal.WrappedException Compiled from "WrappedException.java" public class sun.org.mozilla.javascript.internal.WrappedException extends sun.or g.mozilla.javascript.internal.EvaluatorException{ static final long serialVersionUID; public sun.org.mozilla.javascript.internal.WrappedException(java.lang.Throwa ble); public java.lang.Throwable getWrappedException(); public java.lang.Object unwrap(); }
Ok it exists, so here's some test code:
package testapp;
import sun.org.mozilla.javascript.internal.WrappedException;
public class Main {
public static void main(String[] args) {
WrappedException e = new WrappedException(null);
}
}
Netbeans output:
init: deps-clean: Updating property file: C:\Documents and Settings\LordQuackstar\Desktop\TestApp\build\built-clean.properties Deleting directory C:\Documents and Settings\LordQuackstar\Desktop\TestApp\build clean: init: deps-jar: Created dir: C:\Documents and Settings\LordQuackstar\Desktop\TestApp\build Updating property file: C:\Documents and Settings\LordQuackstar\Desktop\TestApp\build\built-jar.properties Created dir: C:\Documents and Settings\LordQuackstar\Desktop\TestApp\build\classes Created dir: C:\Documents and Settings\LordQuackstar\Desktop\TestApp\build\empty Compiling 1 source file to C:\Documents and Settings\LordQuackstar\Desktop\TestApp\build\classes C:\Documents and Settings\LordQuackstar\Desktop\TestApp\src\testapp\Main.java:8: package sun.org.mozilla.javascript.internal does not exist import sun.org.mozilla.javascript.internal.WrappedException; C:\Documents and Settings\LordQuackstar\Desktop\TestApp\src\testapp\Main.java:16: cannot find symbol symbol : class WrappedException location: class testapp.Main WrappedException e = new WrappedException(null); ^ C:\Documents and Settings\LordQuackstar\Desktop\TestApp\src\testapp\Main.java:16: cannot find symbol symbol : class WrappedException location: class testapp.Main WrappedException e = new WrappedException(null); ^ 3 errors C:\Documents and Settings\LordQuackstar\Desktop\TestApp\nbproject\build-impl.xml:528: The following error occurred while executing this line: C:\Documents and Settings\LordQuackstar\Desktop\TestApp\nbproject\build-impl.xml:261: Compile failed; see the compiler error output for details. BUILD FAILED (total time: 0 seconds)
Command line output:
C:\Documents and Settings\LordQuackstar\Desktop\TestApp\src\testapp>javac Main.java Main.java:3: package sun.org.mozilla.javascript.internal does not exist import sun.org.mozilla.javascript.internal.WrappedException; ^ Main.java:7: cannot find symbol symbol : class WrappedException location: class testapp.Main WrappedException e = new WrappedException(null); ^ Main.java:7: cannot find symbol symbol : class WrappedException location: class testapp.Main WrappedException e = new WrappedException(null); ^ 3 errors
So what would cause this to fail all of a sudden? It was working just fine yesterday. I didn't change anything besides importing 2 more classes from the same package. None of my dependencies changed.
Will test in linux to see if the problem still exists.
Before you say it: No I'm not download rhino separatly, No I'm not changing IDEs,
回答1:
There are two indications that you shouldn't use this class: sun
and internal
- these mean that this is some internal class that shouldn't be used by third parties. Because it can change or be removed in future releases - i.e. this is not part of an API. So - download Rhino separately.
If you are using the scripting API - use only the API classes/interfaces - i.e. javax.script
回答2:
I agree w/ the above advice that you're better off not trying to use the sun internal packages.
This begs the question, how do you access JavaScript arrays w/out sun.org.mozilla.javascript.internal.NativeArray?
What worked for me is code as follows. This creates a Java array called vars based off a JavaScript array called vars.
int varsLength = ((Double)engine.eval("vars.length;")).intValue();
Object[] vars = new Object[varsLength];
for(int i=0; i<vars.length; i++){
vars[i] = engine.eval("vars["+i+"];");
}
回答3:
I had the same error. You must manually add rt.jar
from JRE dir to project libraries. Only this solution seems work. You can also see a tutorial on this approach here by Rob Di Marco
回答4:
This is an old question now, however when I had this problem, my solution was to do more work in the JavaScript environment and then to return a primitive type (String / Boolean) rather than an object.
Of course, this will not satisfy everyone and all requirements, but it may help in some cases.
来源:https://stackoverflow.com/questions/3176112/cant-import-sun-org-mozilla-javascript-internal-in-netbeans