6

I have a jar called "MyTools". The jar is in c:\data folder. I created a new file in the same folder called "UseTools.java". Now I would like to use some of the classes from the MyTools.jar in my UseTools.java. I tried this but it doesnt seem to work:

import MyTools.*;    
public class UseTools
{
  public static void main(String[] args) 
  {
    MyTools.SomeClass foo = new SomeClass();
    SomeClass.doSomething();
  }
}

I tried to compile this with:

javac -cp . UseTools.java

and got this error message:

UseTools.java:1: package MyTools does not exist
import MyTools.*;
^
UseTools.java:7: package MyTools does not exist
        MyTools.SomeClass foo = new SomeClass()
                                     ^
2 errors

I did not set the package name in any class.

Do I have to set a package name in my jar classes?

vikasde
  • 5,681
  • 10
  • 45
  • 62
  • Can you run 'jar -tf MyTools.jar' on the command line and post the output? Perhaps your jar isn't packaged correctly. – karoberts Feb 23 '10 at 17:29
  • i created the jar using netbeans. when I do a -tf it returns a META-INF folder and the SomeClass.class in the root. – vikasde Feb 23 '10 at 17:37
  • first off package names in Java should be all lower case. class names should be CamelCase. –  Feb 23 '10 at 18:00
  • SomeClass.class in the root is wrong, it should be in a directory called MyTools – karoberts Feb 23 '10 at 18:49

3 Answers3

6

To mention something that relates more to the title of the question: In Java, you can't access classes in the default package from code within a named package.

This means, if the classes in your jar file do not belong explicitly to any package and inside the jar your files are directly in the root folder without subfolders, they are in the default package. This is not very elaborated and lacks modularity as well as extensibility, but is technically alright. Then, you can only use these classes from code which also is in the default package. But this does not necessarily mean it has to be in the same jar. If you have multiple src or class folders they could all contain classes in the default package which can interact. The organization in JAR files and the package structure in your project are independent of each other.

However, I'd strictly encourage you to use explicit package information.

mkraemerx
  • 1,713
  • 2
  • 17
  • 21
1

In your MyTools.jar there should be a package with the name MyTools. And before compiling you should add the jar to the classpath.

GuruKulki
  • 25,776
  • 50
  • 140
  • 201
  • ok and when I run it using java, do I still need to include the jar in my classpath? – vikasde Feb 23 '10 at 17:48
  • if you are running through command console then the classpath which you have set will exist till you close the console, so if you run in the same console where you have set the classpath during compilation then no need to set it again. – GuruKulki Feb 23 '10 at 18:05
0

You need to add -cp file.jar instead of -cp .

The latter one will pick up .class files only. BTW: why not using an IDE like netbeans, eclipse or intelliJ?

Karussell
  • 17,085
  • 16
  • 97
  • 197
  • I tried this but get the same error "Package does not exist": javac -cp MyTools.jar UseTools.java – vikasde Feb 23 '10 at 17:07