Java Jar 中忽略的参数

发布于 2024-10-01 14:45:40 字数 852 浏览 1 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

没有伤那来痛 2024-10-08 14:45:40

查看 META-INF/MANIFEST.MF 文件的内容;确保您的Main-Class 使用正确的类。

Have a look at the contents of your META-INF/MANIFEST.MF file; make sure your Main-Class is using the correct class.

×纯※雪 2024-10-08 14:45:40

您的清单指定 mkpkg.MyTest 作为主类文件,而您实际发布的文件的名称为 mypkg.MyTest

另外,您还指定了“.”的类路径。在您的清单中,这充其量是多余的,但可能会导致您看到的问题(因为您的本地目录中可能有一个名为 mkpkg 的目录)。

Your manifest specifies mkpkg.MyTest as the main class file, while the file you've actually posted has the name mypkg.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).

难以启齿的温柔 2024-10-08 14:45:40

如果您知道您的主类,则无需使用 -jar 选项即可完成此操作。

java -classpath .:my_jar_file.jar; package.MainClass [arguments]

这对我在 Debian Lenny 上有用。

If you knew your main class you can do it without the -jar option.

java -classpath .:my_jar_file.jar; package.MainClass [arguments]

This is working for me on Debian Lenny.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文