Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run a Java project in command line

Tags:

java

eclipse

I have a Java project, which runs fine in Eclipse. Right now, I need to run it using command line, like java classpath. How do I setup the classpath based on the stored ones using in Eclipse?

like image 408
bit-question Avatar asked May 27 '11 13:05

bit-question


2 Answers

Say you have changed directories to be in your project directory, and directly underneath that are a bin directory that has your compiled classes and a lib directory that has your jar files. Also let's say the class with the main method you want to invoke is com.initech.example.EntryPoint. On windows you can run:

java -cp bin;lib\*.jar com.initech.example.EntryPoint

The slashes go the other way for Unix, obviously, and you use colon instead of semicolon as a separator in the cp switch.

Using -jar to run your project only works if your classes are packaged in a jar file (duh) and the jar file has a manifest that gives the entry point.

like image 77
Nathan Hughes Avatar answered Sep 21 '22 11:09

Nathan Hughes


Simply navigate to the directory that the class file exists in and use

java -classpath . myClass

Edit: You can replace the . with any classpath. For example, to find your classpath you can use echo %CLASSPATH%

Edit: Looks like there's quite a bit of information that might help you here.

like image 44
alexcoco Avatar answered Sep 21 '22 11:09

alexcoco