How to run Java programs from the terminal?

29,090

If you are referencing any external libraries, then you have to add them to the classpath. You can add it during compilation of the classes this way.

Go to the src directory and :

javac -classpath ".:<path_to_jar_file>" teamL/*.java

TO execute :

java -cp ".:<path_to_jar_file>" teamL.<class_name>

if your are using eclipse, then go to <project_directory>/bin/ here you can find the compiled classes (so you dont have to compile them) and directly run them using the above java command

Note: Since your classes are packaged under teamL package, you have to run the classes from outside the package by specifying the fully qualified name like teamL.ServiceEndpoint

Share:
29,090
Ava
Author by

Ava

Beginner!

Updated on September 30, 2020

Comments

  • Ava
    Ava over 3 years

    I am trying to run a Java program from my Terminal. I have Mac OS X 10.7.

    teamL javac -jar kxml2-2.3.0.jar XMLHandler.java ServiceEndpoint.java TeamL.java

    This is my Eclipse class file structure:

    eclipse class file structure

    I am not able to find why is this throwing Unable to access jarfile kxml2-2.3.0.jar?

    • Neifen
      Neifen over 12 years
      for me it looks like it doesn't find the jarfile
    • Rakesh
      Rakesh over 12 years
      @Vinisa, Accept the answer and close this question
  • Rakesh
    Rakesh over 12 years
    You cant use -jar for compilation, -jar is used to execute a jar application directly. I don't know where exactly your project folder is ! I guess you must be knowing how to change the current directory using the cd command in the terminal. Just cd to Documents/adsproject/ADS_Deliverable1/src/ in the terminal, then javac -classpath ".:<path_to_jar_file>" teamL/*.java
  • Rakesh
    Rakesh over 12 years
    ok looking at the screenshot, i see your project directory is home/Users/vsingh3/Documents/adsproject/ADS_Deliverable1 open a new terminal, then type cd Documents/adsproject/ADS_Deliverable1/src Then javac -classpath ".:Documents/adsproject/ADS_Deliverable1/<path_to_jar_file>" teamL/*.java
  • Ava
    Ava over 12 years
    Thanks but I just noticed your comment about if you are running on eclipse. So, I guess I need not compile. Another thing is that since my jar file was not found so I copy pasted it in each of the folders. I have all my source files and jar file in ADS_Deliverable1->src->teamL. I am now running it like > teamL java -cp -jar kxml2-2.3.0.jar XMLHandler.java ServiceEndpoint.java TeamL.java Why is this throwing Exception in thread "main" java.lang.NoClassDefFoundError: kxml2-2/3/0/jar Caused by: java.lang.ClassNotFoundException: kxml2-2.3.0.jar
  • Ava
    Ava over 12 years
    The same is working in windows eclipse correctly. Also why can't I run the same from my eclipse?
  • Rakesh
    Rakesh over 12 years
    As i told earlier, don't use -jar to add jar files into the classpath ! to make it easier, copy the jar file inside the src folder, then javac -cp . teamL.<main_class> so if ServiceEndpoint is your main class, then you have to use java -cp . teamL.ServiceEndpoint lemme know if you could run this successfully..
  • Ava
    Ava over 12 years
    Why is there a . after cp and I have my jar file in src and in teamL so I tried java -cp . teamL.kxml2-2.3.0.jar teamL.XMLHandler teamL.ServiceEndpoint teamL.TeamL and java -cp . kxml2-2.3.0.jar teamL.XMLHandler teamL.ServiceEndpoint teamL.TeamL. I am inside teamL directory. Getting ClassNotFoundException for jar file.
  • Rakesh
    Rakesh over 12 years
  • Ava
    Ava over 12 years
    Figured it out using eclipse itself with the help of @Rakesh