39

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 :)

2 Answers 2

31

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

Sign up to request clarification or add additional context in comments.

2 Comments

How can I do this for a war? My project takes a lot of time to compile whole project. Is there any way I can recompile a single java file with changes gets added to the already created target folder?
@manikanta nvsr war is a collection of jars. So you rebuild a jar and then replace it in the war file
20

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 ;)

4 Comments

He says he doesn't have the source, but it sounds like he does have the jars. I think he should be ok without mocks.
I'm not getting it to work. I have a java file (Client.java), and it's in this subfolder: net/client/Client.java The jar file is in the root directory. It gives errors at the ; behind the imports.
This means the jars it could find, don't have the package you are using. Most likely your list of jars is incorrect. Note: on ms-dos you need to use ; instead of :
It is only one jar file, so that shouldn't cause any problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.