简体   繁体   English

在不使用IDE的情况下用Java创建.jar文件

[英]Create .jar files in java without using IDE

I am trying to create .jar files in java which can be used by including it in other projects. 我正在尝试在Java中创建.jar文件,可以通过将其包含在其他项目中来使用该文件。 It doesn't contain any main but only classes and methods. 它不包含任何主要类,而仅包含类和方法。 I am using th following command to create .jar file from command line 我正在使用以下命令从命令行创建.jar文件

jar cf firstjar.jar *

I am including the jar file in my project but still the class is not available in the project. 我在项目中包含jar文件,但该类在项目中仍然不可用。 What's the problem in this. 这有什么问题。

My guess is that you've got a problem with the directory structure. 我的猜测是您的目录结构有问题。

Suppose you have a class like this: 假设您有一个像这样的课程:

package org.foo;

public class Bar {}

There should be Bar.class flie in a directory org/foo . org/foo目录中应该有Bar.class That structure has to be in the jar file as well - so you should go to the directory containing org and run: 该结构也必须在jar文件中-因此,您应该转到包含 org的目录并运行:

jar cf firstjar.jar org

(Or whatever your top-level package name is.) (或者您的顶级软件包名称是什么。)

If you just include the classfiles from the foo directory then Java's expectations of where to find things will be invalid. 如果仅包含foo目录中的类文件那么Java对在哪里找到对象的期望将是无效的。

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

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