Including jars in classpath on commandline (javac or apt)

JavaJar

Java Problem Overview


trying to run this program. I think that to setup all of the web service stuff I need to run apt. (Although using javac I am having the same issue). I think what I am getting is compile errors. (Shown at bottom).

I think what I need to do is include this jar in my class path: jsr181-api.jar http://community.jboss.org/message/338674#338674">(source)</a>;. Is there a simple temporary way to do this (on solaris)? I don't want to add it to my bash_rc file (it is there forever). I also know that there is some way to do it using a manifest text file but that seemed complicated so I didn't look into it yet. Can I just do something like:

javac HelloImp <listOfJars>

or

ant HelloImp <listOfJars>

Code:

package server;

import javax.jws.WebService;

@WebService
public class HelloImpl {

  /**
   * @param name
   * @return Say hello to the person.
   */
   public String sayHello(String name) {
     return "Hello, " + name + "!";
   }
}

Compile errors:

HelloImpl.java:3: package javax.jws does not exist
import javax.jws.WebService;
                 ^
HelloImpl.java:5: cannot find symbol
symbol: class WebService
@WebService
 ^
2 errors

Update: Cool that is wrapped up but it is still not quite working. I have created a https://stackoverflow.com/questions/2096437/apt-warning-no-annotation-processor-found"> new question to keep things nice and organized:

Java Solutions


Solution 1 - Java

Try the following:

java -cp jar1:jar2:jar3:dir1:. HelloWorld

The default classpath (unless there is a CLASSPATH environment variable) is the current directory so if you redefine it, make sure you're adding the current directory (.) to the classpath as I have done.

Solution 2 - Java

In windows:

java -cp C:/.../jardir1/*;C:/.../jardir2/* class_with_main_method

make sure that the class with the main function is in one of the included jars

Solution 3 - Java

Note for Windows users, the jars should be separated by ; and not :.

for example: javac -cp external_libs\lib1.jar;other\lib2.jar;

Solution 4 - Java

Use the -cp or -classpath switch.

$ java -help  
Usage: java [-options] class [args...]  
           (to execute a class)  
   or  java [-options] -jar jarfile [args...]  
           (to execute a jar file)  

where options include:  
...  
    -cp <class search path of directories and zip/jar files>  
    -classpath <class search path of directories and zip/jar files>  
                  A ; separated list of directories, JAR archives,  
                  and ZIP archives to search for class files.  

(Note that the separator used to separate entries on the classpath differs between OSes, on my Windows machine it is ;, in *nix it is usually :.)

Solution 5 - Java

> javac HelloWorld.java -classpath ./javax.jar , assuming javax is in current folder, and compile target is "HelloWorld.java", and you can compile without a main method

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionsixtyfootersdudeView Question on Stackoverflow
Solution 1 - JavaKevinView Answer on Stackoverflow
Solution 2 - JavaBenView Answer on Stackoverflow
Solution 3 - JavaluisvView Answer on Stackoverflow
Solution 4 - Javamatt bView Answer on Stackoverflow
Solution 5 - JavaStoica MirceaView Answer on Stackoverflow