java 执行JAR文件总报 Could not find the main class
来源:互联网 发布:计算机编程经典书籍 编辑:程序博客网 时间:2024/06/06 09:58
经过调查研究,执行JAR文件时候JAVA会忽略所有的CLASSPATH设置,所以才会报这个错误,解决办法
1.把所有的依赖的库一同打包到JAR文件中,并且JAR文件的MAIN-FEST文件里面定义CLASSPATH
2.把JAR文件放到CLASSPATH路径中,执行JAVA CLASS_NAME
详细的原理和资料参考下面的内容:
java -cp classpath
Specify a list of directories, JAR archives, and ZIP archives to search for class files. Class path entries are separated by colons (:). Specifying -classpath or -cp overrides any setting of the CLASSPATH environment variable.
As a special convenience, a class path element containing a basename of * is considered equivalent to specifying a list of all the files in the directory with the extension .jar or .JAR (a java program cannot tell the difference between the two invocations).
For example, if directory foo contains a.jar and b.JAR, then the class path element foo/* is expanded to a A.jar:b.JAR, except that the order of jar files is unspecified. All jar files in the specified directory, even hidden ones, are included in the list. A classpath entry consisting simply of * expands to a list of all the jar files in the current directory. The CLASSPATH environment variable, where defined, will be simi-larly expanded. Any classpath wildcard expansion occurs before the Java virtual machine is started -- no Java program will ever see unexpanded wildcards except by querying the environment. For example; by invoking System.getenv("CLASSPATH").
For more information on class paths, see Setting the Class Path.
查看文档得到以上信息,以下是查找到的资料:
-cp 参数后面是类路径,是指定给解释器到哪里找到你的.class文件,
写法:
java -cp .;myClass.jar packname.mainclassname
classpath中的jar文件能使用通配符,如果是多个jar文件,要一个一个地罗列出来,从某种意义上说jar文件也就是路径。
要指定各个JAR文件具体的存放路径,相同路径有多个可使用通配符
java -cp .;c:/classes/myClass.jar;d:/classes/*.jar packname.mainclassname
packname.mainclassname为包含main方法的完全限定类名,如果在classpath中有多个还有main方法的类,通过此命令可以方便选定程序的入口。
例如:
#!/bin/sh
cd /home/work/bvpn/aaa/bin
pwd
nohup /usr/java/jdk1.6.0_11/bin/java -cp .:../lib/log4j-1.2.15.jar:/usr/java/jdk1.6.0_11/lib/dt.jar:/usr/java/jdk1.6.0_11/lib/tools.jar:/usr/java/jdk1.6.0_11/lib/:/usr/java/jdk1.6.0_11/lib/jradius-client.jar:../lib/mysql-connector-java-5.1.12-bin.jar authAgent.AuthAgentMain 2>&1 &
下面是一些遇到的问题:
1.jar -cp lib/referenced.jar -jar myworks.jar会执行报错,原因如下:
我们使用-jar选项的话java.exe会忽略-cp,-classpath,以及环境变量CLASSPATH的参数。
解决方法一:
不要使用-jar选项,直接调用含有main方法的class文件,这样-cp,-classpath以及环境变量里的CLASSPATH指定的参数就都能使用到了。
java -classpath ./lib/junit.jar:. test/Test1
解决方法二:
继续使用-jar选项,但是在MAINFEST.MF文件中指定引用到jar文件.
Class-Path: myplace/myjar.jar myplace/other.jar jardir/
另外说明一点:这个问题可能有些人遇不到,因为Java的版本不同的原因,我在Sun的JDK和IBM 1.5的JDK都遇到了这个问题,但是对于 IBM 1.4的JDK却没有类似问题
2. 批量加载包的方法:
(一)java命令引入jar时可以-cp参数,但时-cp不能用通配符(多个jar时要一个个写,不能*.jar),通常的jar都在同一目录,且多于1个。前些日子刚刚发现一个参数-Djava.ext.dirs~
如:java -Djava.ext.dirs=lib MyClass 【同样的weblogic中就有这样的参数:-Dweblogic.ext.dirs=$BEA_HOME/patch_weblogic920/profiles/default/sysext_manifest_classpath】
(二)通过Unix shell设置CLASSPATH方式加载(shell实在是方便)
CLASSPATH=`find ../lib -name *.jar|xargs|sed "s/ /:/g"`
CLASSPATH=".:$CLASSPATH
(三)cp 参数后面是类路径,是指定给解释器到哪里找到你的.class文件。使用java -cp命令可以调用一个其他目录下的带有main函数的类。格式为java -cp class文件的路径,下面的例子采用的相对路径。
String commandLine = "java -cp "+ "..\\kwicByPipeServer\\bin KWIC";
Process p = Runtime.getRuntime().exec(commandLine);
这样就可以调用了。
PS:
$BEA_HOME/wlserver_10.3/server/lib/weblogic.jar的classcompatibilityinspector-file-0.xml文件记录了可用的类~- java 执行JAR文件总报 Could not find the main class
- jar文件Could not find the main class简易解决办法
- jar文件Could not find the main class解决办法
- fatjar导出的jar文件双击执行时提示could not find the main class
- could not find the main class win7/xp 双击执行jar包出现:could not find the main class
- could not find the main class win7/xp 双击执行jar包出现:could not find the main class
- eclipse打的jar包无法执行,报错could not find main class
- win7/xp 双击执行jar包出现:could not find the main class
- win7/xp 双击执行jar包出现:could not find the main class
- windows 双击执行jar包出现:could not find the main class
- jar出错 could not find the main class
- Java执行main方法,异常为:could not find the main class.program will exit
- 启动Tomcat报错:Could not find the main class
- 解决Win7/XP中双击jar文件出现could not find the main class的方法
- 运行jar文件时,出现“could not find the main class”的解决方法
- 报错could not find the main class:java.overWrite.overWrite.Program will exit.的解决方法
- Java中Could not find the main class.program will
- [Java Beginner] Problem to "Could not find the main class"
- 如何关掉webview中select弹出的对话框。
- GNS+SecurityCRT
- Ubuntu 环境变量
- WIN XP下pppoe服务器的搭建与使用
- OFFICE2007工具栏显示设置
- java 执行JAR文件总报 Could not find the main class
- Linux 前后台进程切换
- 高通平台上的AMSS(Modem端) windows环境搭建
- go语言使用crc32得到网络消息的校验码
- 打印前20个平凡数(某公司校园招聘笔试题目)
- ASP.NET 在线编辑、调用Word
- 自己开发类似Vega中ObjectPropertyEditor的工具
- 如何做好预付帐款流程
- ssh免密码登录(个人总结)