How to run Java programs from the terminal?
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
Comments
-
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:
I am not able to find why is this throwing
Unable to access jarfile kxml2-2.3.0.jar
?-
Neifen over 12 yearsfor me it looks like it doesn't find the jarfile
-
Rakesh over 12 years@Vinisa, Accept the answer and close this question
-
-
Rakesh over 12 yearsYou 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 thecd
command in the terminal. Justcd
toDocuments/adsproject/ADS_Deliverable1/src/
in the terminal, thenjavac -classpath ".:<path_to_jar_file>" teamL/*.java
-
Rakesh over 12 yearsok looking at the screenshot, i see your project directory is
home/Users/vsingh3/Documents/adsproject/ADS_Deliverable1
open a new terminal, then typecd Documents/adsproject/ADS_Deliverable1/src
Thenjavac -classpath ".:Documents/adsproject/ADS_Deliverable1/<path_to_jar_file>" teamL/*.java
-
Ava over 12 yearsThanks 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 throwingException in thread "main" java.lang.NoClassDefFoundError: kxml2-2/3/0/jar Caused by: java.lang.ClassNotFoundException: kxml2-2.3.0.jar
-
Ava over 12 yearsThe same is working in windows eclipse correctly. Also why can't I run the same from my eclipse?
-
Rakesh over 12 yearsAs i told earlier, don't use
-jar
to add jar files into theclasspath
! to make it easier, copy the jar file inside the src folder, thenjavac -cp . teamL.<main_class>
so ifServiceEndpoint
is your main class, then you have to usejava -cp . teamL.ServiceEndpoint
lemme know if you could run this successfully.. -
Ava over 12 yearsWhy is there a
.
after cp and I have my jar file in src and in teamL so I triedjava -cp . teamL.kxml2-2.3.0.jar teamL.XMLHandler teamL.ServiceEndpoint teamL.TeamL
andjava -cp . kxml2-2.3.0.jar teamL.XMLHandler teamL.ServiceEndpoint teamL.TeamL
. I am insideteamL
directory. GettingClassNotFoundException
for jar file. -
Rakesh over 12 years
-
Ava over 12 yearsFigured it out using eclipse itself with the help of @Rakesh