How can I import package1 in Test2.java ?C:\Users\nirjari\Desktop\package2\Test2.java:1: error: package package1 does not exist
import package1.*;
^
C:\Users\nirjari\Desktop\package2\Test2.java:6: error: cannot find symbol
Test1 t1= new Test1();
^
symbol: class Test1
location: class Test2
C:\Users\nirjari\Desktop\package2\Test2.java:6: error: cannot find symbol
Test1 t1= new Test1();
^
symbol: class Test1
location: class Test2
3 errors
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand."
--- Martin Fowler
I don't understand. I have put code here in first post. package2 and package1 are under test dir.How is Test2 packaged?
C:\Users\nirjari\Desktop\test>java -cp "C:\Users\nirjari\Desktop\test\package2" Test2
Exception in thread "main" java.lang.NoClassDefFoundError: Test2 (wrong name: package2/Test2)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$100(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)
You don't need that classpath, but as GG-M points out, the name of the class is package2.Test2, not Test2. The stack trace even says “wrong name”.German Gonzalez-Morris wrote:try with:
java -cp "C:\Users\nirjari\Desktop\test" package2.Test2
What on earth makes you think that is not good practice. It is usually better to import named classes than import‑on‑demand.Tushar Goel wrote: . . .
import package1.ClassName ; // But this is not good practice
. . .
When I run this code in eclipse, it works fine without any error. But when I run it on local system (creating classes in text files and running it manually) , it does not work.
Thanks.
It worked when I run it as
C:\Program Files\Java\jdk1.7.0_45\bin>java -cp "C:\Users\nirjari\Desktop\test" package2.Test2
But why do I have to run it as package2.Test2 ? Whenever I provide a package to a class, do I have to execute this class with package name ?
It does not work when I execute without classpath
Yes because compiler will search your class in path mentioned in permanent path variable or in local path variable. If it does not found it there then it throws error.
C:\Program Files\Java\jdk1.7.0_45\bin>java package2.Test2
Error: Could not find or load main class package2.Test2
At what path your package is? You are trying to run it at java tool path.
I think it should be at C:\Users\nirjari\Desktop\test\
And you should never put your own code in the Java installation folder. You should have a folder you are working in, and put all your work in that.
Start by undefining the system classpath. That usually does more harm than good.
Yes, but record it somewhere carefully in case there is something which needs a system classpath. Quicktime is one example, and it is notorious for messing up your Java® programs by altering the classpath. Itr is probably worth showing us your classpath, please.nirjari patel wrote: . . . You mean to say remove CLASSPATH variable from system variables ?
. . .
Itr is probably worth showing us your classpath, please