老问题:在命令行下执行jar包的问题

Sanco 2006-03-08 11:39:47
testOne.jar 需要用到 testTwo.jar

在命令行中用:
java -jar testOne.jar (试过用-classpath 但是不起作用)

所以通过testOne中的Manifest.mf加入
Main-Class: testOne.Main
Class-Path: testTwo.jar

情况是:
1。如果我把它们放在同一目录下,运行正常。
2。不在同一目录,在Class-Path行使用-相对目录,运行正常
3。不在同一目录,在Class-Path行使用-绝对目录,运行不正常,提示找不到Main Class。

因为如果这个包要访问很多其他目录下的包,这就很麻烦了。
难道都要copy到同一目录下吗?

注意:现在在命令行下作,不使用任何IDE(用Eclipe这点很容易作到)
...全文
1908 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
Sanco 2006-03-08
  • 打赏
  • 举报
回复
还是没用。不过还是感谢 “我爱花猫”
sportboys 2006-03-08
  • 打赏
  • 举报
回复
sorry for my poor knowledge on the absolute path and relative path...
pdvv 2006-03-08
  • 打赏
  • 举报
回复
java -jar -Dclasspath=testTwo.jar testOne.jar

指定classpath参数肯定可以!

另外关于绝对路径问题,参见:http://java.sun.com/developer/Books/javaprogramming/JAR/basics/manifest.html

Class-Path: servlet.jar infobus.jar acme/beans.jar
With this header, the classes in the files servlet.jar, infobus.jar, and acme/beans.jar will serve as extensions for purposes of the applet or application. The URLs in the Class-Path header are given relative to the URL of the JAR file of the applet or application.

只能用相对路径。
Sanco 2006-03-08
  • 打赏
  • 举报
回复
错误信息如下:
Exception in thread "main" java.lang.NoClassDefFoundError: testOne/Main

用相对路径则没问题。
Sanco 2006-03-08
  • 打赏
  • 举报
回复
/和\两种情况都试过了,没有任何区别,除非用相对路径。
sportboys 2006-03-08
  • 打赏
  • 举报
回复
often we put all the third-party jars, utils jars into one or two folders named lib.

so you can add them into classpath in the manifest.mf of the target jar.
I don't think it won't work with absolute path, remember the difference between \ and / in Windows.

62,626

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧