Java Jar 中忽略的参数
我在 Ubuntu 10.10 上使用 OpenJDK 1.6.0_18 运行以下代码:
package mypkg;
public class MyTest {
public static void main(final String[] args) {
System.out.println(args.length + " argument(s)");
for (final String arg : args) {
System.out.println(arg);
}
}
}
将其编译成 Jar 后,我完全困惑为什么从终端执行以下命令会返回 0 argument(s)
:
java -jar mytest.jar 这是一个测试
这是我对 Java 文档,指出:
java [选项] -jar file.jar [参数...]
我几乎感觉我在终端中输入了错误的命令。什么给?
编辑: MANIFEST.MF 包含:
Manifest-Version 1.0
Created-By: 1.6.0_18 (Sun Microsystems Inc.)
Main-Class: mypkg.Starter
Class-Path: .
I'm running the following code on Ubuntu 10.10, using OpenJDK 1.6.0_18:
package mypkg;
public class MyTest {
public static void main(final String[] args) {
System.out.println(args.length + " argument(s)");
for (final String arg : args) {
System.out.println(arg);
}
}
}
After compiling it into a Jar, I'm completely puzzled why executing the following command from the terminal returns 0 argument(s)
:
java -jar mytest.jar this is a test
This is my interpretation of the Java docs, stating:
java [ options ] -jar file.jar [ argument ... ]
I almost have the feeling that I'm entering a wrong command in the terminal. What gives?
Edit: the MANIFEST.MF contains:
Manifest-Version 1.0
Created-By: 1.6.0_18 (Sun Microsystems Inc.)
Main-Class: mypkg.Starter
Class-Path: .
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
查看
META-INF/MANIFEST.MF
文件的内容;确保您的Main-Class
使用正确的类。Have a look at the contents of your
META-INF/MANIFEST.MF
file; make sure yourMain-Class
is using the correct class.您的清单指定
mkpkg.MyTest
作为主类文件,而您实际发布的文件的名称为mypkg.MyTest
。另外,您还指定了“.”的类路径。在您的清单中,这充其量是多余的,但可能会导致您看到的问题(因为您的本地目录中可能有一个名为
mkpkg
的目录)。Your manifest specifies
mkpkg.MyTest
as the main class file, while the file you've actually posted has the namemypkg.MyTest
.Also, you specify a Classpath of "." in your manifest, which is superfluous at best, but probably leads to the problem you see (as you've probably got a directory named
mkpkg
in your local directory).如果您知道您的主类,则无需使用 -jar 选项即可完成此操作。
这对我在 Debian Lenny 上有用。
If you knew your main class you can do it without the -jar option.
This is working for me on Debian Lenny.