简体   繁体   English

如何编译单个Java文件

[英]How to compile a single Java file

I have searched this, but I could'n find or understand what I found. 我搜索了这个,但我无法找到或理解我发现的东西。
Now I'm not a Java programmer, but I have the need to compile a single Java file into an existing (compiled) Java program. 现在我不是Java程序员,但是我需要将一个Java文件编译成现有的(已编译的)Java程序。 The source of this Java code is not available to me, therefore I cannot compile the entire project. 无法使用此Java代码的源代码,因此无法编译整个项目。
I'm not interested in decompiling the original project. 我对反编译原始项目不感兴趣。

How to quickly do this using only the JDK and javac? 如何使用JDK和javac快速完成此操作? (Through the command line is what I prefer.) (通过命令行是我更喜欢的。)

I understand that to do so error checking outside of the single java file will have to be disabled, because it can't read the dependencies. 我知道要这样做,必须禁用单个java文件之外的错误检查,因为它无法读取依赖项。

Thanks in advance, 提前致谢,
-Aidiakapi -Aidiakapi

EDIT: I do have the JAR file, thanks for the answer :) 编辑:我有JAR文件,谢谢你的答案:)

As far as I can understand you want to re-compile a single java file and replace it in an existing jar file.. 据我所知,你想重新编译一个java文件并将其替换为现有的jar文件。

So you compile it.. 所以你编译它..

cmd>javac -classpath jar1.jar;jar2.jar my.company.MyClassToReplace.java

and replace it in the jar. 并在罐子里更换它。

cmd>jar uf myJarFile.jar my/company/MyClassToReplace.class

You need to have the jar(s) which contains all the things your class depends on to compile it. 你需要让jar包含你的类所依赖的所有东西来编译它。

You can then compile the Class with 然后,您可以使用编译类

javac -classpath jar1:jar2 mypackage.MyNewClass

If you have no access to the original Jars, you will have to create mock classes and method etc (which don't have to do anything, just be there so your class compiles) Using an IDE can make both processes easier. 如果您无法访问原始Jars,则必须创建模拟类和方法等(它们不必执行任何操作,只需在那里进行编译)使用IDE可以使这两个过程更容易。 (That is what it is for ;) (这就是它的用途;)

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

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