Fare riferimento ad una classe contenuta in un file Jar

From RVM Wiki
Jump to navigation Jump to search

Let's Say we need to use the class

   Classname

that is contained in the jar file

   org.example.jar

And your source is in the file

   mysource.java

Like this:

   import org.example.Classename;
   public class mysource {
       public static void main(String[] argv) {
       .....
      }
   }

First, as you see, in your code you have to import the classess you need:

   import org.example.Classename;

Second, when you compile the source, you have to reference the jar file.

Please note the difference in using

   :

and

   ;

If you are under a unix like operating system:

   javac -cp '.:org.example.jar' mysource.java

If you are under windows:

   javac -cp .;org.example.jar mysource.java

After this, you obtain the bytecode file

   mysource.class

Now you can run this.

If you are under a unix like operating system:

   java -cp '.:org.example.jar' mysource

If you are under windows:

   java -cp .;org.example.jar mysource

Riferimenti