在 Eclipse 中打包到 jar

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15523046/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 20:12:52  来源:igfitidea点击:

Package to jar in eclipse

javaeclipsejarpackage

提问by jsj

UPDATE: Don't worry. User Error. Would delete the question but cant with answers.

更新:别担心。用户错误。会删除问题,但不能提供答案。

I am trying to use eclipse to create a jar of packages, nota runnable jar, which I can import into another project.

我正在尝试使用 eclipse 创建一个包的jar,而不是一个可运行的 jar,我可以将其导入到另一个项目中。

So far I have done this:

到目前为止,我已经这样做了:

  1. Create a new project
  2. Create a package in that project (pkg.nme.with.dots)
  3. Create a class in that package
  4. Export -> Java -> Jar
  1. 创建一个新项目
  2. 在该项目中创建一个包 (pkg.nme.with.dots)
  3. 在该包中创建一个类
  4. 导出 -> Java -> Jar

The resultant .jar file has only one directory META-INF, and three files .project, .classpath, and MyClass.class.

得到的.jar文件只有一个目录META-INF,和三个文件.project.classpathMyClass.class

If I try to import pkg.nme.with.dots.MyClass;I get a package not found error, despite adding the full path of the jar to my CLASSPATH.

如果我尝试import pkg.nme.with.dots.MyClass;,尽管将 jar 的完整路径添加到我的 CLASSPATH 中,我仍会收到未找到包的错误。

I tried adding the jar as an external lib in eclipse but it is still not recognised.

我尝试将 jar 添加为 eclipse 中的外部库,但仍然无法识别。

Obviously I have exported it into a format that can't be imported. How do I export it correctly?

很明显我已经导出成无法import编辑的格式了。如何正确导出?

回答by greenkode

@trideceth12 I just tested exporting a sample package in my eclipse. and my folder structure works fine. not sure why yours is not working properly. you should check all your export settings. here's what mine looks like, and it exports fine.enter image description here

@trideceth12 我刚刚测试了在我的 eclipse 中导出一个示例包。我的文件夹结构工作正常。不知道为什么你的不能正常工作。您应该检查所有导出设置。这是我的样子,而且出口很好。在此处输入图片说明

回答by Mr.Cool

when you create jar file using eclipse choose the export option ,in your jar creation wizard chosse your necessary folder ,then finish, for see here

当您使用 Eclipse 创建 jar 文件时,选择导出选项,在您的 jar 创建向导中选择您需要的文件夹,然后完成,参见 此处

回答by Himanshu Bhardwaj

Look like your Java source is in Default package only. If you want to use this kind of import statement, considering moving the jar file to:

看起来您的 Java 源代码仅在默认包中。如果要使用这种导入语句,可以考虑将 jar 文件移动到:

pkg/nme/with/dots