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?

MrSmith42
10.2k6 gold badges41 silver badges52 bronze badges
asked Mar 26, 2011 at 9:54

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
Gonen
4,1201 gold badge34 silver badges42 bronze badges
answered Mar 26, 2011 at 10:58
Sign up to request clarification or add additional context in comments.

7 Comments

can you do it without JAR - ing?
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).
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: Probably best to ask this as a separate question with more detail - I'm sure you'll get an answer.
how the hell is this supposed to be building an eclipse project from CLI -_-
|
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
The Unfun Cat
32.5k32 gold badges127 silver badges168 bronze badges
answered Mar 26, 2011 at 14:01

3 Comments

"very easy" looks different
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.
Semicolon is used on Windows as it was mentioned in my answer. On Linux and MacOS you have to use colon.
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/

answered Mar 26, 2011 at 10:08

1 Comment

isn't there a way of doing this without any additional tools? doesn't eclipse provide some commands for doing this?
3

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

answered Jun 24, 2014 at 21:09

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.