如何将类文件添加到新的 Java 项目

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1486551/
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-08-12 13:22:00  来源:igfitidea点击:

How to add class-files to new java project

javanetbeans

提问by slashmais

I'm tasked with converting an existing Java/C++ mixed web-application to pure Java, but I'm hampered by some missing Java sources (.java files) for which only the class-files are available. Fortunately I don't need to change anything in that code, just need to continue calling the methods.

我的任务是将现有的 Java/C++ 混合 web 应用程序转换为纯 Java,但我受到一些缺少的 Java 源(.java 文件)的阻碍,这些源代码只有类文件可用。幸运的是,我不需要更改该代码中的任何内容,只需要继续调用这些方法。

I created a new Java Web Application project (using Netbeans); recreated the applet by copying it's sources in and got it working in a skeletal fashion, with the calls to classes & methods not in the sources commented out, but I am now stuck on how to add the class-files (of the missing sources) to this project.

我创建了一个新的 Java Web 应用程序项目(使用 Netbeans);通过复制它的源代码来重新创建小程序并使其以骨架方式工作,对源代码中没有的类和方法的调用被注释掉了,但我现在被困在如何添加类文件(丢失的源代码)到这个项目。

(I'm new to Java, obviously) Any pointers on how I should proceed will be most welcome.

(显然,我是 Java 新手)任何有关我应该如何进行的指示都将受到欢迎。

采纳答案by Pascal Thivent

Package the .classfiles in a jar.

.class文件打包成jar.

$ jar cvf my-library.jar  the/package/*.class

Add this jar to the CLASSPATHof your project/application. In Netbeans:

将此 jar 添加到CLASSPATH您的项目/应用程序的 。在 Netbeans 中:

  • go to the project view on your left
  • then right click on the library option,
  • then click add JAR/Folder option.
  • 转到左侧的项目视图
  • 然后右键单击库选项,
  • 然后单击添加 JAR/文件夹选项。

回答by enguerran

To add some source/code to your project, classic java project or webapp java project, you have to declare the path to the needed class/jar in the classpath variable.

要将一些源代码/代码添加到您的项目、经典 java 项目或 webapp java 项目,您必须在 classpath 变量中声明所需类/jar 的路径。

If you are running your dynamic web project via eclipse, just add the path to the classpath tab in the "run configurations" of your server.

如果您通过 Eclipse 运行动态 Web 项目,只需将路径添加到服务器“运行配置”中的类路径选项卡。

To learn more about classpath, see wikipedia.

要了解有关类路径的更多信息,请参阅wikipedia

回答by Aaron Digulla

The usual approach is to collect all these class files in a JAR file (use the jartool) and put them on the classpath.

通常的方法是将所有这些类文件收集在一个 JAR 文件中(使用该jar工具)并将它们放在类路径中。

回答by Aaron Digulla

Different from C/C++, for Java you don't need the source code to these other pieces in order to compile the applet. There is enough information in the class file for the Java compiler to do what needs to be done.

与 C/C++ 不同,对于 Java,您不需要这些其他部分的源代码来编译小程序。类文件中有足够的信息供 Java 编译器执行需要执行的操作。

So you can uncomment the calls to this code and follow the instructions in the other posts to put the class files on your project classpath.

因此,您可以取消对此代码调用的注释,并按照其他帖子中的说明将类文件放在您的项目类路径中。

回答by OliBlogger

In the Project window, right click on your project and select Properties. Go to the Libraries category. Then click Add JAR/Folder and select the location of your .class files.

在“项目”窗口中,右键单击您的项目并选择“属性”。转到库类别。然后单击添加 JAR/文件夹并选择 .class 文件的位置。