I have a Java project, which runs fine in Eclips开发者_如何学Goe. 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:
- Export the java project in to a Runnable jar - using Eclipse IDE
- Select the main or running class file - Launch configuration
- In Library handling - select the option [ Extract required libraries in to jar file ]
- Open command prompt go to the directory where runnable jar is available
- 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, 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.
精彩评论