aspose file tools*
The moose likes Beginning Java and the fly likes Lost with JAR files and packages 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 "Lost with JAR files and packages" Watch "Lost with JAR files and packages" New topic
Author

Lost with JAR files and packages

Christian Polychroniadis
Greenhorn

Joined: May 18, 2008
Posts: 8
HI,

I am a little lost with the packages and JAR Files, I made a tree structure:

c:>
/java
/myProyect
/source/
AnimalTest.java
Animal.java
Dog.java

So, I compile, AnimalTest.java Without package sentence, and Everything is fine.

But when I specified a package for AnimalTest.java, the compiler could not find the others classes, even when they are in the same package, this is the code:

AnimalTest.java

package java.myProyect.source;
import java.myProyect.*;

class AnimalTest{

public static void main(String args[]){
Animal a=new Animal();
Dog d=new Dog();
a.eat();
d.eat();
}
}


Animal.java
package java.myProyect.source;
public class Animal{
public void eat(){
System.out.println("Animal Comiendo");
}
}

Dog.java
package java.myProyect.source;
public class Dog extends Animal{
public void eat(){
System.out.println("Perro Comiendo");
}
}

And The other thing is that if I make a .JAR, Called, MyJar.jar, I can't even compile the AnimalTest.java, file I had tried, standing in the folder java:

c:>\java\javac -cp C:>java/MyJar.jar AnimalTest.java

And it did not work.

I apreciate any help, thanks.


Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18133
    
  39

Keep in mind that the classpath specifies the *root* of the java file -- not the location of the java file. So, if the package is...

package java.myProyect.source;

And the classpath is by default, the current directory, you need to be in the root directory.

c:\> javac java\myProyect\source\AnimalTest.java

c:\> java java.myProyect.source.AnimalTest

And BTW, the main purpose of packages is to avoid name collisions. It is not to just enforce a directory location. It is unusual to have packages with "source", as you probably named that to specify the source directory.

Henry



Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Christian Polychroniadis
Greenhorn

Joined: May 18, 2008
Posts: 8
Hello Henry, you are right, I changed the package name, it was just for testing purposes, but it worked, Thank you.

Now, if i make a .jar file with all the files, and I just have the jar file, how Can I compile the AnimalTest.java file, and How can I run it.

Supose that I put the jar file in the root dir, in this case is C:>, and I put all the folders, what Do I need to do ?

C:>jar -cf MyJar.jar java

and then

C:>javac MyJar.jar AnimalTest.java ?

and to run it ?

C:>java MyJar.jar AnimalTest ?

Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18133
    
  39

The default classpath of the current directory, is only valid if you don't specify a classpath. Once you specify a classpath, it will use the classpath that you specified. So, if you want the current directory to be in the class path, you will need to add it...

Furthermore, package rules still apply... So, assuming that the AnimalTest class is in the Jar file, then you need....

c:\> java -cp MyJar.jar java.myProyect.source.AnimalTest

And if AnimalTest is not in the jar file, then you need...

c:\> java -cp MyJar.jar;. java.myProyect.source.AnimalTest


And if you are wondering that packages may make you command line long, there are ways to shorten it... for example, if you add a main-class attribute to your manifest, you can actually do this...

c:\> java -jar MyJar.jar

Henry
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Lost with JAR files and packages
 
Similar Threads
Class not found?
classpath
casting
Classpaths with packages
Accessing inherited protected instance variable question