tags:

views:

118

answers:

2

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?

A: 

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
I tried this but get the same error "Package does not exist": javac -cp MyTools.jar UseTools.java
vikasde
+1  A: 

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

GK
ok and when I run it using java, do I still need to include the jar in my classpath?
vikasde
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.
GK