14

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?

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
bit-question
  • 3,733
  • 17
  • 50
  • 63

6 Answers6

12

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.

alexcoco
  • 6,657
  • 6
  • 27
  • 39
  • After typing echo $CLASSPATH$, the system just outputs $ – bit-question May 27 '11 at 14:06
  • 1
    In Linux environment variables start with a $ but do not end with one. So to see the value of the variable `$CLASSPATH` try `echo $CLASSPATH`. You won't see anything unless you have set the variable to start with though. See my answer for how to do this, at least in bash. I'm not familiar with zsh. – Ben May 27 '11 at 14:31
  • If your classpath is complicated, e.g. because of many libraries, use this answer to get the classpath: http://stackoverflow.com/a/23622375/1921273 – MrSmith42 May 26 '16 at 07:46
5

How to run a java project from command line using Runnable jar

Using Eclipse you can easily run a java program but using Runnable jar is slightly different. Steps to run a java project:

  1. Export the java project in to a Runnable jar - using Eclipse IDE
  2. Select the main or running class file - Launch configuration
  3. In Library handling - select the option [ Extract required libraries in to jar file ]
  4. Open command prompt go to the directory where runnable jar is available
  5. type > java -jar Runnable.jar
Mardoz
  • 1,617
  • 1
  • 13
  • 26
user3959717
  • 51
  • 1
  • 1
4

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.

Nathan Hughes
  • 94,330
  • 19
  • 181
  • 276
  • Hi, the system returns as "zsh: no matches found: bin:lib/*.jar" – bit-question May 27 '11 at 14:27
  • Have you changed to your project directory? Are there subdirectories called bin and lib? The details will of course depend on your set-up. – Ben May 27 '11 at 14:34
  • @bit-question: do you have any jars in your eclipse project? if not you can shorten it to '-cp bin'. Or if you have jars, try losing the wildcard and list the jars separately. – Nathan Hughes May 27 '11 at 15:02
  • @bit-question: zsh is trying to resolve the wildcard, put the cp switch argument in quotes. – Nathan Hughes Jun 01 '11 at 18:26
  • I think you just have to use *, not *.jar. So your example, under linux, would be `java -cp bin:lib/* com.initech.example.Entrypoint` – localhost Dec 03 '14 at 05:04
3

Select the project and click on File->Export which opens a new window.

From that select Runnablejar option and click next button.

In launch configuration select your main class and in export destination give the path where you want to store the jar file.

Now go the command line issue this command java -jar mainclass(classname)

1
jre\bin\java -jar JarFileName.jar

This will allow you to run a jar on windows from command line.

Buhake Sindi
  • 87,898
  • 29
  • 167
  • 228
RMT
  • 7,040
  • 4
  • 25
  • 37
0

You can set the classpath using the -classpath or -cp option of the java command, or you can set the $CLASSPATH environment variable before launching your application, e.g.

export CLASSPATH=bin:lib/*
java -jar packagename.Application

Either way you should probably write a script so that you can start the application without multiple steps or typing a long command. Or you could use ant, or export a runnable Jar from Eclipse. It really depends why you need to do this.

To find what entries are needed in the classpath, it is probably simplest just to look at the .classpath file created by Eclipse in the root directory of the project.

Ben
  • 2,348
  • 2
  • 19
  • 28