Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 943
Aidiakapi Avatar asked Feb 11 '11 14:02

Aidiakapi


People also ask

How do I compile a Java file?

Open a command prompt window and go to the directory where you saved the java program. Assume it's C:\. Type 'javac MyFirstJavaProgram. java' and press enter to compile your code.

How do I compile a single Java file in eclipse?

Your class has to define a public static void main(String[] args) method. Then find the Java file in the project explorer, right-click on it, and choose "run as Java application". The file name has to be the same (+ extension ".

How do I compile and run a Java file?

Type 'javac MyFirstJavaProgram. java' and press enter to compile your code. If there are no errors in your code, the command prompt will take you to the next line (Assumption: The path variable is set). Now, type ' java MyFirstJavaProgram ' to run your program.

How do I run a single Java file in a project?

One way to do this would be to add a method public static void main(String[] args) containing the code you want to run. Once you've done this, you can right-click on it, choose the "Run as..." option, and select "Java application" to run the program you've written.


1 Answers

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

like image 98
Oleg Pavliv Avatar answered Oct 08 '22 01:10

Oleg Pavliv