简体   繁体   English

JAR 中的类文件似乎被忽略了; 而是从类路径上的目录中提取类文件

[英]class file in JAR seems to be ignored; class file being picked up from directory on classpath instead

I am trying to get a simple JAR file executing on the CLI (without a manifest).我正在尝试在 CLI 上执行一个简单的 JAR 文件(没有清单)。 The folder structure is as follows:文件夹结构如下:

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

Here is Main.java :这是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.我查看了 JAR zip,看起来还不错。 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()). MANIFEST.MF 是默认的,但这没关系,因为我明确地告诉 Java 从哪个类开始(带有 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.所以看起来 JAR 中的类文件根本没有被拾取。 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.如果我在运行java时省略了out文件夹,或者如果我重命名了out文件夹中的.class文件),我会收到 class not found 错误。 I would like to get it working without a manifest.我想让它在没有清单的情况下工作。

Any ideas?有任何想法吗?

Thanks, Seán.谢谢,肖恩。

您需要更改到 'out' 目录以使您的包根在 jar 中正确:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM