JAR中的类文件似乎被忽略了;类文件是从classpath上的目录中挑选的

发布于 2025-02-06 10:16:52 字数 1188 浏览 2 评论 0原文

我正在尝试在CLI上执行一个简单的JAR文件(没有清单)。文件夹结构如下:

mods\un.jar
out\test\Main.class
src\test\Main.java

这是main.java

package test;

public class Main{
  public static void main(String []args){
    System.out.println("Unnamed module...");

  }
}

这是来自根文件夹的命令:

javac -d out src\Main.java
java -cp out test.Main      - this works
jar -cvf mods\un.jar out\test\Main.class
java -cp mods\un.jar;out test.Main   - works (when 'out' is in classpath)
java -cp mods\un.jar test.Main - ERROR

最后一行生成错误:

Error: Could not find or load main class test.Main
Caused by: java.lang.ClassNotFoundException: test.Main

我已经查看了罐子拉链,看起来还可以。 ustest.mf是默认一个,但这是可以的,因为我明确地告诉java从shot(main()类)开始。内容是:

META-INF\MANIFEST.MF
out\test\Main.class

清单本身是:

Manifest-Version: 1.0
Created-By: 11 (Oracle Corporation)

因此看起来罐子中的类文件根本没有被捡起。如果我在运行java时省略out文件夹,或者如果我在out out 文件夹中重命名.class文件),我发现没有发现错误。我想在没有明显的情况下使它起作用。

有什么想法吗?

谢谢, seán。

I am trying to get a simple JAR file executing on the CLI (without a manifest). The folder structure is as follows:

mods\un.jar
out\test\Main.class
src\test\Main.java

Here is Main.java:

package test;

public class Main{
  public static void main(String []args){
    System.out.println("Unnamed module...");

  }
}

Here are the commands from the root folder:

javac -d out src\Main.java
java -cp out test.Main      - this works
jar -cvf mods\un.jar out\test\Main.class
java -cp mods\un.jar;out test.Main   - works (when 'out' is in classpath)
java -cp mods\un.jar test.Main - ERROR

The last line generates the errors:

Error: Could not find or load main class test.Main
Caused by: java.lang.ClassNotFoundException: test.Main

I have looked at the JAR zip and it looks ok. The MANIFEST.MF is the default one but that is okay as I am explicitly telling Java what class to start with (the class with main()). The contents are:

META-INF\MANIFEST.MF
out\test\Main.class

The manifest itself is:

Manifest-Version: 1.0
Created-By: 11 (Oracle Corporation)

So it looks like the class file in the JAR is not being picked up at all. If I omit the out folder when I run java or if I rename the .class file in the out folder), I get the class not found error. I would like to get it working without a manifest.

Any ideas?

Thanks,
Seán.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

寻找一个思念的角度 2025-02-13 10:16:52

您需要更改为“ OUT”目录,以使您的软件包在JAR中正确正确:

jar -cvf mods\un.jar -C out test\Main.class

You need to change to the 'out' directory to get your package root correct in the jar:

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