主类不能设置为现有类

发布于 2024-10-15 19:54:13 字数 330 浏览 5 评论 0原文

我有一个构建设置,要求将我的类放入当前目录的子目录中的文件夹中。 IE,如果我位于“.”,那么类可能位于“./somewhere_else/”中。

问题是,当我这样做时,我无法为可以找到主类的 Main-Class 设置值。当我从“.”处的文件构建 jar 时,我没有问题,但以下所有主类尝试都会导致 java.lang.NoClassDefFoundError's:

Main-Class: ClassName
Main-Class: FolderName.ClassName
Main-Class: FolderName/ClassName

我应该使用什么?

I have a build setup that requires that my classes be put into a folder in a subdirectory of my current directory. I.E., if I'm at '.', then the classes could be in './somewhere_else/'

The problem is that when I do this I can't set a value for Main-Class that can find the main class. I have no problem when I build the jar from files at '.', but all the following attempts for main class result in java.lang.NoClassDefFoundError's:

Main-Class: ClassName
Main-Class: FolderName.ClassName
Main-Class: FolderName/ClassName

What should I be using?

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

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

发布评论

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

评论(3

时光暖心i 2024-10-22 19:54:13

jar 中的类必须与包结构位于相同的目录结构中。

  • 所以,如果你想让你的类ClassName(没有任何包)成为主类,它必须位于你的jar的根目录中。
  • 如果您希望该类位于目录 FolderName 中,则该名称必须是包名称,这意味着源文件的第一行(非空非注释)应该是 包文件夹名称;

如果您的问题只是构建设置 - 这不是问题,jar 外部目录的文件夹布局不一定与 jar 内部的布局相同。但如果文件夹结构适合包结构(可能还有上面的目录),它仍然有帮助。

The classes inside your jar have to be in the same directory structure as your package structure.

  • So, if you want to have your class ClassName (without any package) to be the main class, it has to be in the root directory of your jar.
  • If you want to have the class in the directory FolderName, this same name has to be the package name, meaning the first (non-empty non-comment) line of your source file should be package FolderName;.

If your problem is only the build setup - this is not a problem, the folder-layout of your directories outside the jar does not necessarily have to be the same as the layout inside your jar. But it still helps if the folder-structure fits to the package-structure (with maybe additionally directories above).

淡莣 2024-10-22 19:54:13

我认为你应该简单地通过类名(没有目录结构)来引用Main-Class。将帮助您解决这个问题的是您放置的类路径。确保Main-Class目录位于类路径中。

I think you should reference Main-Class simply by the class name (without directory structure). What will help you solve this problem is what you put as the classpath. Make sure the directory of Main-Class is in the classpath.

爱情眠于流年 2024-10-22 19:54:13

如果您的 ClassName 位于包 FolderName 中,那么它应该是:-

Manifest-Version: 1.0
Main-Class: FolderName.ClassName
Class-Path: <third-party>.jar <another-third-party>.jar

顺便说一句,它们区分大小写。

If your ClassName resides in package FolderName, then, it should be:-

Manifest-Version: 1.0
Main-Class: FolderName.ClassName
Class-Path: <third-party>.jar <another-third-party>.jar

They are case-sensitive, by the way.

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