简体   繁体   中英

How to run a Java project in command line

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?

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 .

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

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.

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)

jre\bin\java -jar JarFileName.jar

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

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, eg

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM