How to compile a single Java file

亡梦爱人 提交于 2019-12-17 22:10:02

问题


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. The source of this Java code is not available to me, therefore I cannot compile the entire project.
I'm not interested in decompiling the original project.

How to quickly do this using only the JDK and 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.

Thanks in advance,
-Aidiakapi

EDIT: I do have the JAR file, thanks for the answer :)


回答1:


You need to have the jar(s) which contains all the things your class depends on to compile it.

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. (That is what it is for ;)




回答2:


As far as I can understand you want to re-compile a single java file and replace it in an existing jar file..

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



来源:https://stackoverflow.com/questions/4970182/how-to-compile-a-single-java-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!