aspose file tools*
The moose likes Beginning Java and the fly likes Issue when compiling a class which is importing a package Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Issue when compiling a class which is importing a package" Watch "Issue when compiling a class which is importing a package" New topic
Author

Issue when compiling a class which is importing a package

madhu kongara
Greenhorn

Joined: Mar 16, 2011
Posts: 1
Dear All,

I am new to java and I am facing an issue when compiling a class which is importing a package.

Please find the below steps which I am doing.

Step1:

I created the below addition.java in a folder c:\java



so now I have addition.java in c:\java\addition.java
I also have a folder called pack inside the c:\java folder.


step2: Now compile the addition.java class
javac -d pack addition.java
After compilation when I can see the addition.class in c:\java\pack\subpack\innersubpack\ folder.
Until now it is fine

Step3: Now create a class which is trying to import the addition class



This UseAddition.java is saved in c:\java folder.


Step4:
Now when I am trying to compile the code it is failing
c:\java> javac UseAddition.java
UseAddition.java:1: package subpack.innersubpack does not exist
import subpack.innersubpack.*;

My question is How to compile this UseAddition.java code without moving the code into the c:\java\pack folder and keep it in the c:\java folder itself and compile.

Appreciate your help here.

Thanks&Regards
Madhu K.
amit punekar
Ranch Hand

Joined: May 14, 2004
Posts: 507
Hello,
First of all keep the "addition.java" in appropriate package folder.
In your case it would be C:\Java\subpack\innersubpack\addition.java.

Secondly when you want to use that in another class i.e. UserAddition, you will need to set the classpath so that the "addition.class" can be located when dependancies are resolved. To do this set the classpath to the "addition.java".
This can be done by using

Please note that execute this command in "C:\java" so that "." will set the classpath to current directory.
The "UserAddition" class is not in any package so you have correctly placed the same in C:\Java, which should not be a problem.

Regards,
Amit
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36578
    
  16
amit punekar wrote: . . . Secondly when you want to use that in another class i.e. UserAddition, you will need to set the classpath so that the "addition.class" can be located . . .
Are you sure? I think that is unnecessary. Compiling classes in packages causes no end of confusion, so I have been to some old posts of mine and altered what I said earlier
Amended from what I previously wrote:to create a package: two ways to do it.

All your classes must have the package declared as the very first statement, before "import" statements.

1: Create a folder with the same name (all lower-case) as the package, and put your java files in that. Navigate to the folder one level up from your package, and compile the classes like this
javac packagename/MyClass.java jackagename/MyClass2.java
You still need to compile supplier classes before client classes.

2: Don't create a folder; navigate to the folder one level up from your package, which contains the classes, and compile the classes like this
javac -d . MyClass.java MyClass2.java
The same thing about supplier classes first applies.

To execute, navigate to the folder one level up (if you used method 2 you will already be there) and write java.mypackage.MyClassWithMainMethod command line arguments
Method 1: no folders, only the three .java classes.
campbell@queeg:~/java$ javac -d . Class1.java Class2.java Class3.java
campbell@queeg:~/java$ java pack1.Class1
null null
campbell@queeg:~/java$ java pack1.Class2
null null
campbell@queeg:~/java$ java pack2.Class3
null null
Method 2: Delete all the .class files, move the 3 .java files into their respective directories
campbell@queeg:~/java$ javac pack1/Class1.java pack1/Class2.java pack2/Class3.java
campbell@queeg:~/java$ java pack1.Class1
null null
campbell@queeg:~/java$ java pack1.Class2
null null
campbell@queeg:~/java$ java pack2.Class3
null null
Delete the Class3.class file and navigate to pack2 . . . and it fails because it can't find pack1
amit punekar
Ranch Hand

Joined: May 14, 2004
Posts: 507
Hi there,
May be I did not word it properly.
What I meant to say that because UserAddition.java was not placed in the package where "addition.java" is put into, you will need to set the classpath so that class can be located.
May be I was not clear while writing the answer.
thanks for elaboration though.

Regards,
amit
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36578
    
  16
They are not in a different directory structure. You need to execute and compile multiple packages from the root of their directory structure.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36578
    
  16
I managed to get it to compile like this: Set a directory structure. At this point there are no .class files:
[campbell@campbell1110 addition]$ javac subpack/innersubpack/addition.java UseAddition.java&
[1] 15070
[campbell@campbell1110 addition]$ java UseAddition
The sum is 5.801
[1]+ Done
. . . and I hadn't noticed you were new the the Ranch. Sorry.

Welcome
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Issue when compiling a class which is importing a package
 
Similar Threads
Import static
Create a package for source file "Vector.java"
Is explicit import(without *) required for user defined classes?
Issue adding double value
"Double" and "double" - what's the difference?