• Post Reply Bookmark Topic Watch Topic
  • New Topic

importing package in another package is giving error  RSS feed

 
nirjari patel
Ranch Hand
Posts: 386
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
C:\Users\nirjari\Desktop\package1\Test1.java

C:\Users\nirjari\Desktop\package2\Test2.java

Test1.java source code
This code compiles and Test1.class is created in same location where Test1.java is

Test2.java source code
Compiling Test2.java generates following error message
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
How can I import package1 in Test2.java ?

Thanks
 
Partheban Udayakumar
Ranch Hand
Posts: 499
AngularJS Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nirjari,

Do you have both the packages in the same project or different projects. Try moving both the packages to a common folder (say test) as
C:\Users\nirjari\Desktop\test\package1
C:\Users\nirjari\Desktop\test\package2

and try compiling it.
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How is Test2 packaged?
 
nirjari patel
Ranch Hand
Posts: 386
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's the error I get when I put package1 and package2 under test package.

How is Test2 packaged?
I don't understand. I have put code here in first post. package2 and package1 are under test dir.

Thanks
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you should compile it from the test directory and use javac package2\Test2.java
And you will get a different compilation error because of the poor design of your classes. Unfortunately the compiler won't notice the poor names of your identifiers
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, if the Test2.java file is in the package2 directory, it should include a package name
 
nirjari patel
Ranch Hand
Posts: 386
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. Test2.jaava did compile without any error. But when I run this class, I am getting following error. Why is that ? Test2.class is in the classpath specified and it does have main method
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)



 
German Gonzalez-Morris
Ranch Hand
Posts: 311
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try with:

java -cp "C:\Users\nirjari\Desktop\test" package2.Test2
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
German Gonzalez-Morris wrote:try with:

java -cp "C:\Users\nirjari\Desktop\test" package2.Test2
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”.
 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
#nirja .. Problem is in with your second code. You created your second code in default package and then try to import first package. In this case compiler will consider that package1 is also in default package and will search there. But when it is not found any package there then it throws error mentioned error. There are 2 ways to avoid this :

1) in import statement give class name as well. like

import package1.ClassName ; // But this is not good practice

2) Not to put java file parallel to the your package. Means move your java file in different folder. Just keep package in that folder.

Let me know if still having any doubt.
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch
Tushar Goel wrote: . . .
import package1.ClassName ; // But this is not good practice
. . .
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
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am talking about specifically to the case when we tried to import some package into the default package.

In general importing specific class is always good. No doubt into this.
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't import packages; you import classes or their static members. It doesn't make any different whether the import declaration is in a class in a named package or the unnamed package.
 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okies.. Thanks i will keep that in mind.. By the way whether that problem is resolved or not ?
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know whether it is resolved. The OP hasn't come back to say yea or nay.
 
nirjari patel
Ranch Hand
Posts: 386
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am sorry for getting back bit late here.

C:\Users\nirjari\Desktop\test\package1
C:\Users\nirjari\Desktop\test\package2

Thats how both packages are created. So when you say default package, what do you mean by that ?

If I create code in eclipse IDE

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.
C:\Program Files\Java\jdk1.7.0_45\bin>java -cp "C:\Users\nirjari\Desktop\test\package2" Test2

Thanks for your replies and sorry for posting back so late.

Thanks

 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All those files which are not created in user package will be created in default package. It is a concept. You can not print its name.

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.


Did you set the class path when tried to run it on local machine.

 
nirjari patel
Ranch Hand
Posts: 386
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
C:\Program Files\Java\jdk1.7.0_45\bin>java package2.Test2
Error: Could not find or load main class package2.Test2

CLASSPATH variable is set as C:\Users\nirjari\Desktop\test;

Thanks
 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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


Welcome !!!


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 ?


Because when you compiled *.java file then it create byte code of that file. This byte code having information of package you created and if you run the byte code without package name then it does not found any package and give you compilation error which you are getting when you tried without package name. Once you mentioned the package name it full fill the condition and your class run.

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.
 
nirjari patel
Ranch Hand
Posts: 386
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

If CLASSPATH environment variable is set as "C:\Users\nirjari\Desktop\test;" , then why does compiler not found it there ? As class file is in this path only.

Thanks
 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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\ .

Need to run command at there itself.
 
nirjari patel
Ranch Hand
Posts: 386
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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\

"C:\Users\nirjari\Desktop\test\ " this path I have set as CLASSPATH. So do I still need to go to this path ?
Secondly, if I go to this path , then java command gives error. I have set PATH variable as "C:\Program Files\Java\jdk1.7.0_45\bin", but still when I run java command from different dir, java exe file is not found.
I have also set JAVA_HOME as "C:\Program Files\Java\jdk1.7.0_45\bin"

Thanks
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That JAVA_HOME is wrong. It should not have \bin in.
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
nirjari patel
Ranch Hand
Posts: 386
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Code is not in Java installation folder. Its in a different folder, I am working in.

Thanks for JAVA_HOME clarification. It is working after setting JAVA_HOME. Even if JAVA_HOME is wrong, if I have also set PATH variable as "C:\Program Files\Java\jdk1.7.0_45\bin", why does it not work ?
Is it because I have define JAVA_HOME variable ? If I don't have JAVA_HOME variable defined, then will this PATH variable work ? (I tested it and when I deleted JAVA_HOME, PATH variable works. But if I have wrong JAVA_HOME variable, it does not take PATH variable value). Does it mean that if I have JAVA_HOME variable defined, then it has to correct, otherwise even if PATH variable is set correct, code will give error ?

Secondly, if I have defined CLASSPATH as "C:\Users\nirjari\Desktop\test;" , and if I execute code from a different dir as
C:\>java package2.Test2 , shall it not work ? (Actually, It does not)

My understanding is if I have CLASSPATH defined, then from whichever dir I execute java command, CLASSPATH will be automatically taken from this variable. If its not so, then whats point in setting CLASSPATH and if it is so, then why does this command not work ?

Thanks
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Start by undefining the system classpath. That usually does more harm than good.
 
nirjari patel
Ranch Hand
Posts: 386
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Start by undefining the system classpath. That usually does more harm than good.

I don't understand what do you mean by this ? You mean to say remove CLASSPATH variable from system variables ?

By the way, I did some work on my end. When I have CLASSPATH and PATH defined, and I am accessing CLASS files, which are not in a package, it works fine as follows

"C:\>java BasicTest
This is Basic Test"

Now when I am using a class file defined in a package, which is in same path, I am getting error.

"C:\>java package2.Test2
Error: Could not find or load main class package2.Test2"

Why is that ?

So do I need to run class file within a package from dir where package is ?

Thanks
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nirjari patel wrote: . . . You mean to say remove CLASSPATH variable from system variables ?
. . .
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.
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need a different classpath for each application, but those small apps don't need a classpath at all. The the JVM uses “.” i.e. the current directory as its default classpath.
You should execute the Test2 program from the folder which includes the package2 folder.
 
nirjari patel
Ranch Hand
Posts: 386
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Itr is probably worth showing us your classpath, please

CLASSPATH is set as "C:\Users\nirjari\Desktop\test;"
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Delete that classpath. Go to the test directory and make sure it contains the two package1 and package2 directories.
Make sure Test1.java and Test1.class are in package1 and Test2.java and Test2.class are in package2. You will of course have to add the package name package2 to Test2.java
Change the two Strings to private access and give them getFName/getLName methods. Call those methods in Test2
Compile with javac package1/Test1.java package2/Test2.java
Run with java package2.Test2
 
nirjari patel
Ranch Hand
Posts: 386
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does that mean you have got it to work?

And … you're welcome
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!