日期:2014-05-20  浏览次数:20796 次

dos下运行java程序的问题
系统的classpath和path环境变量已设置好,现在在eclipse新建一项目,并引用到外部的jar文件(放在lib下),编译运行正常。

现在我不需要在eclipse下运行了,我需要直接在dos下运行,运行java RunClient (RunClient是main函数的类名)。提示找不到类,正是lib下的jar的类。

请问这是该如何设置运行时项目引用到的lib所指向路径呢?

------解决方案--------------------
我的异常网推荐解决方案:java环境变量设置
------解决方案--------------------
建立一个bat文件(内容如下):
set classpath=%classpath%; 你的累库路径下的.jar
java RunClient
------解决方案--------------------
加上可选项 好像是-cp *.jar RunClient
这里的*.jar是你要用到的那些类库
------解决方案--------------------
classpath=.;C:\Java\jdk1.5.0_04\lib
path=c:\java\jdk1.5.0_04\bin
java_home=c:\Java\jdk1.5.0_04

JAVA_HOME=c:\Java\jdk1.5.0_04
PATH=c:\java\jdk1.5.0_04\bin

classpath=.;D:\j2sdk1.4.2_04\lib\dt.jar;D:\j2sdk1.4.2_04\lib\tools.jar;
------解决方案--------------------
java -cp {*.jar} 带包名的类名 (例如,com.demo.RunClient)
{*.jar} 代表需要的Jar在当前目录下,如果不在当前目录下,需要指明路径。

或者,将常用的Jar配置导环境变量中,省的每次都指定比较麻烦。
或者,直接做成一个bat文件,每次直接双击即可运行。

java命令,格式说明:
Usage: java [-options] class [args...] (to execute a class)
or java [-options] -jar jarfile [args...] (to execute a jar file)

where options include:
-client to select the "client" VM
-server to select the "server" VM
-hotspot is a synonym for the "client" VM [deprecated]
The default VM is client.

-cp <class search path of directories and zip/jar files>
-classpath <class search path of directories and zip/jar files>
A ; separated list of directories, JAR archives,
and ZIP archives to search for class files.
-D<name>=<value>
set a system property
-verbose[:class|gc|jni]
enable verbose output
-version print product version and exit
-version:<value>
require the specified version to run
-showversion print product version and continue
-jre-restrict-search | -jre-no-restrict-search
include/exclude user private JREs in the version search
-? -help print this help message
-X print help on non-standard options
-ea[:<packagename>...|:<classname>]
-enableassertions[:<packagename>...|:<classname>]
enable assertions
-da[:<packagename>...|:<classname>]
-disableassertions[:<packagename>...|:<classname>]
disable assertions
-esa | -enablesystemassertions
enable system assertions
-dsa | -disablesystemassertions
disable system assertions
-agentlib:<libname>[=<options>]
load native agent library <libname>, e.g. -agentlib:hprof
see also, -agentlib:jdwp=help and -agentlib:hprof=help
-agentpath:<pathname>[=<options>]
load native agent library by full pathname
-javaagent:<jarpath>[=<options>]
load Java programming language agent, see java.lang.instrument

-splash:<imagepath>
show splash screen with specified image
------解决方案--------------------
java -cp client.jar xxx.RunClient
假设你的包名叫client.jar,RunClient的包名为xxx.RunClient


如有有两个以上的jar包,则用分号“;”隔开,如:
java -cp test.jar;client.jar xxx.RunClient

你也可以为JVM分配内存,如:
java -Xms64M -Xmx256M -cp test.jar;client.jar xxx.RunClient

其中,-Xms64M代表最少分配64M内存,-Xmx256M代表最大256M



------解决方案--------------------