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.
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.
Delete the Class3.class file and navigate to pack2 . . . and it fails because it can't find pack1
Joined: May 14, 2004
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.
Joined: Oct 13, 2005
They are not in a different directory structure. You need to execute and compile multiple packages from the root of their directory structure.
Joined: Oct 13, 2005
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&
[campbell@campbell1110 addition]$ java UseAddition
The sum is 5.801
. . . and I hadn't noticed you were new the the Ranch. Sorry.