Can't import sun.org.mozilla.javascript.internal in NetBeans - java

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,

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

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+"];");
}

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

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.

Related

Java Incompatible Types error after placing classes into a package

I have a package named "shapefile" which contains classes IndexFileReader and IndexFile. IndexFileReader returns an object of type IndexFile.
I then have a class named ReadFiles which imports "shapefile" and uses IndexFileReader to create an IndexFile.
import java.io.IOException;
import shapefile.*;
public class ReadFiles {
public static void main(String[] args) throws IOException{
IndexFile indexFile;
indexFile = IndexFileReader.readIndexFile("FilePath/FileName.shx");
}
}
My files are located in folders as follows:
JavaProjects
shapefile
IndexFileReader.java
IndexFile.java
MyProject
ReadFiles.java
I compile the shapefile project from the Windows command line like this and it compiles fine:
cd JavaProjects
javac .\shapefile\*.java
I compile my project like this and I get an error:
cd MyProject
javac -cp .. ReadFiles.java
Error:
ReadFiles.java:8: error: incompatible types
indexFile = IndexFileReader.readIndexFile("FilePath/FileName.shx");
^
required: IndexFile
found: shapefile.IndexFile
This is new to me and I believe I am doing something wrong with running javac. I have successfully compiled ReadFiles previously, I thought I did so with the same commands I've printed here. I actually still have the ReadFiles.class file from when this compiled successfully and it runs without any problems, which is why I believe I am not using javac correctly.
Why is Java giving an error after requiring an object of "IndexFile" and receiving an object of "shapefile.IndexFile"? Aren't these objects the same type?
Why is my IndexFileReader class returning an object of type "shapefile.IndexFile" instead of an object of type "IndexFile"?
Is there anything with how I have my files/folders laid out that will cause problems with compiling?
What command to I provide Windows to correctly use javac to compile my package and to compile my ReadFiles class?
I have looked through the other Incompatible Types questions and I have not found one that had led me in the right direction.

Cannot find symbol error in cmd but not in IDE

I'm trying to compile the following code (one of two files I need to complete this homework) but I'm getting 2 errors in cmd. This is what cmd throws at me:
CarRentalTest.java:12: error: cannot find symbol
CarRental myCarRental = new CarRental(); //create CarRental object CarRental
^
symbol: class CarRental
location: class CarRentalTest
CarRentalTest.java:12: error: cannot find symbol
CarRental myCarRental = new CarRental(); //create CarRental object CarRental
^
symbol: class CarRental
location: class CarRentalTest
2 errors
And this is the code I'm trying to compile.
public class CarRentalTest {
public static void main (String[] args)
{
CarRental myCarRental = new CarRental(); //create CarRental object CarRental
myCarRental.Customers();
} //end method main
} //end class CarRentalTest
What's weird is that the whole thing runs fine in NetBeans. What am I doing wrong here? :9
What am I doing wrong here?
Not building CarRental, or not telling the compiler where to find the class if you have already compiled it. The IDE is probably assuming you want to build everything, so that's fine.
We don't know how your code is organized, but you should either pass all the relevant filenames to the compiler at the same time:
javac -d classes src\CarRental.java test\CarRentalTest.java
... or put the output directory of the earlier compilation in the classpath for the later compilation, e.g.
javac -d classes src\CarRental.java
javac -d testclasses -cp classes test\CarRentalTest.java
If you are using a standard directory layout for your project, where production and test code are in separate directory trees then the java command line will not see the production class if your currect directory is the test directory.
To clarify:
Suppose you have this dir structure:
src/
main/
java/
mypackage/
CarRental.java
test/
java/
mypackpage/
CarRentalTest.java
and you are in the 'src/test/java/mypackage/' directory, you would experience this error when running javac at the command line - although the production and test classes are in the same package, they are in different directories.
The IDE knows about this directory structure, includes the test path during compilation and therefore it works OK.
You need to import CarRental class in the CarRentalTest.
import yourpackage.CarRental in the CarRentalTest. Java Compiler can't find the CarRental in the CarRentalTest.java.
In the IDE whole package comes in the java file
import package.car.*;
This is why it is working in IDE.

Packages, Files and Getting simple code to run!

I have SimpleSphere.java and TestClass.java stored in a folder called MyPackage.
Attempting to compile TestClass gives this error:
TestClass.java:7: error: cannot find symbol
SimpleSphere ball = new SimpleSphere(19.1);
^
symbol: class SimpleSphere
location: class TestClass
TestClass.java:7: error: cannot find symbol
SimpleSphere ball = new SimpleSphere(19.1);
^
symbol: class SimpleSphere
location: class TestClass
2 errors
But I am fairly certain I have everything set up correctly (evidently I do not, and yet I remain stubborn!). Also, even if these two files were not part of MyPackage, shouldn't JAVA look in the current directory as default and find SimpleSphere???
Seems that you're compiling the classes directly using javac ClassName.java inside the folder where they are located. You have to move one folder up and compile them since there.
Here's a sample of how the files should be located
- basePath
- MyPackage
+ SimpleSphere.java
+ TestClass.java
In your cmd/shell:
# [basePath] javac MyPackage/SimpleSphere.java
# [basePath] javac MyPackage/TestClass.java
# [basePath] java MyPackage.TestClass
Try moving one folder up and then compiling.
Best of Luck.

Java file not compiling

Currently trying to work with objects in Java. Everything goes fine until I hit compile. Have been reading a couple of other questions regarding the same problem, or the same given error, and at this point I am not sure wether I am forgetting something or that I need to change my classpath.
Main Class file:
package TesterClass;
public class Tester {
public static void main(String[] args){
TesterClass firstTest = new TesterClass();
firstTest.stringPrinter();
}
}
The file that is supposed to be functioning as a package file:
package TesterClass;
public class TesterClass{
private String workingSegment;
public TesterClass(){
workingSegment = "Working";
}
public void stringPrinter(){
System.out.println(workingSegment);
}
}
The 2 files are in the same directory and I am trying to manually compile them with
"javac Tester.java". The error I get is about the fact that its having issues with the package. All help is welcome!
EDIT: Forgot to post the actual compiler error.
Tester.java:9: cannot find symbol
symbol : class TesterClass
location: class TesterClass.Tester
TesterClass firstTest;
^
Tester.java:11: cannot find symbol
symbol : class TesterClass
location: class TesterClass.Tester
firstTest = new TesterClass();
^
2 errors
Move to the top of the source tree and compile both class...
So, assuming you source files are in \Java\TesterClass, you need to start in \Java
javac TesterClass\Tester.java TesterClass\TesterClass.java
You may also want to have a quick read of Code Conventions for the Java Programming Language as packages names are suppose to be in lower case :P
Updated
I just tried...
javac TesterClass\Tester.java
And it worked fine.
Are you sure that the Tester.java and TesterClass.java are in the TesterClass directory?
Updated with running example
So, basically, I dropped you .java files into the directory \compile under the TesterClass (\compile\TesterClass) directory and compiled them using...
\compile>javac TesterClass\Tester.java
Then I run them...
\compile>java TesterClass.Tester
Working
You need to go to the top of the directory hierarchy and first compile your TesterClass and then compile your Tester. Since you have not compiled your TesterClass yet, Tester is unable to find it.
The error clearly states that its not able to find the symbol TesterClass, and the reason being TesterClass hasn't been compiled yet.
I suggest you use an IDE which does the compilation automatically for you. If you stick to manual compilation, you need to compile all the classes in the proper order.
Try changing the package name so it does not match the class name. Right now they are the same. Make it package TesterClassPackage, then import TesterClass into the file with the main() method. Even though they are in the same package sometimes you need to literally import files even though they are in the same package.
javac TesterClass\TesterClass.java TesterClass\Tester.java
will do it

Different behaviour of netbeans and console

I have written a code in java. In which I have created a package called xml-creator.
Package xml_creator has 3 classes say XML_Control, XML_Creator, and XML_implement.
When I run my project on netbeans (NetBeans 7.0) it works fine. But if I try to compile code on console, I get various errors like
When I compiled XML_Creator.java, I get following errors.
XML_Creator.java:371: cannot find symbol
symbol : variable XML_implement
location: class xml_creator.XML_Creator
typeAttr.setValue(XML_implement.table_col[i][2]);
^
XML_Creator.java:375: cannot find symbol
symbol : variable XML_implement
location: class xml_creator.XML_Creator
for(int j=0;j<XML_implement.kTab;j++)
^
XML_Creator and XML_implemenr both are in same package but non of them extend each other.
I am sorry I cant show code on this site as it is too large and aginst the company's policies.
I dont understand why it is showing me errors?
Sample code
XML_Control.java
package xml_creator;
public class XML_Control
{
public static void main(String as[])
{
XML_Creator xml = new XML_Creator();
}
}
XML_Creator.java
package xml-creator;
public class XML_Creator
{
XML_implement ixml = new XML_implement();
public XML_Creator()
{
System.out.println(""+ixml.a);
}
}
XML_implement.java
package xml_creator;
public class XML_implement
{
public int a;
public XML_implement()
{
a = 10;
}
}
So when I compile XML_Creator.java, console gives error.
It sounds like you're compiling within the directory containing the .java file, and only telling the compiler about one of the source files. That's the problem - to try to find a source or class file, the compiler is using the package name, and expecting the packages to be laid out in the conventional fashion. Compile from the root of the source tree - which I certainly hope you're using - like this:
javac xml_creator/*.java
You may also want to specify an output directory - which again will be the root of the directory hierarchy for packages:
javac -d bin xml_creator/*.java
If you're building regularly from the command-line (and not just for throwaway code) you should look into using a build system such as Ant.

Categories