Java - Build and run eclipse project from command line

Solution 1:

Maven or Ant are the best option but for an Eclipse-only solution
you can choose File -> Export and select Java -> Runnable JAR File
then transfer the JAR file to your other machine and run this from the command line:

java -jar YOUR.JAR

Solution 2:

You can run Java applications from the command line. The simplified syntax looks like this:

java -cp <classpath> <main class> <args>

where:

<classpath> - list of directories and/or JAR-files where needed classes reside separated by ";" for Windows or ":" for linux (default classpath is "." - the current directory);

<main class> - fully qualified name of the class containig main() method (for example, org.myself.HelloWorld)

<args> - various arguments for application if any.

So, if you find the directory where Eclipse stored compiled classes (usually it's bin) you may use the command, like

java -cp . my.package.MyClass

Or, if you use some libraries and classes in other directories, it could be:

java -cp some-cool-lib.jar:another-lib.jar:/some/directory/with/classes my.package.MyClass