50

I have a java project written using eclipse ide and I want to run it through ssh on a different machine, but I have to do this using the command line and I don't know exactly how.

I am a beginner at both shell commands and java.

Could you please give me a useful link with answers regarding this question, or perhaps a set instructions of how to do this?

4 Answers 4

51

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
7
  • 1
    can you do it without JAR - ing? Feb 18, 2013 at 10:56
  • 2
    You can but you would have to copy the contents of the bin directory and any libraries your program uses to your other machine and include them all on your classpath (see xappymah's answer). Mar 13, 2013 at 16:41
  • Great answer, but my problem is that I want to run that jar on a machine that has an older JRE. To do this, under Eclipse preferences I set the compiler compliance level to 1.6 and I create the jar.. but this doesnt solve the problem. Any clue?
    – Alphaaa
    May 23, 2013 at 14:16
  • @Alphaaa: Probably best to ask this as a separate question with more detail - I'm sure you'll get an answer. May 23, 2013 at 15:56
  • 1
    how the hell is this supposed to be building an eclipse project from CLI -_-
    – nonchip
    May 3, 2018 at 13:05
34

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
3
  • 4
    "very easy" looks different Jun 9, 2016 at 12:41
  • With other libraries and classes, the command should use ; (semicolon), not : (colon). So, it should be java -cp some-cool-lib.jar;another-lib.jar;/some/directory/with/classes my.package.MyClass; otherwise, it will say cannot find the main class.
    – Northnroro
    Feb 27, 2019 at 16:52
  • Semicolon is used on Windows as it was mentioned in my answer. On Linux and MacOS you have to use colon.
    – xappymah
    Feb 28, 2019 at 4:45
3

To build and run a Java project, Its good to use an ant or maven tool. you can find many tutorials on google for the same.

a good tutorial on ant is here http://www.intranetjournal.com/java-ant/

1
  • 3
    isn't there a way of doing this without any additional tools? doesn't eclipse provide some commands for doing this?
    – Simon
    Mar 26, 2011 at 10:17
3

This is what I did and it worked for me. Hope it could help. enter image description here

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.