Decompile a Java project and compile


Question

I am trying to decompile a java project(.jar) file and I am able to get .java files from it. Now how can I compile it back? I am able to add the .java files to Netbeans just as a single file.But how can I add it as a project add compile it? The project is a JavaFX project. So please help me on this.

1
0
5/15/2014 5:49:51 AM

Accepted Answer

There are many ways to achieve this, but I don't think you will be able to import the project inside the jar just out of the box ( in this case, out-of-the-jar ) like that.

We can use the most basic technique ( a bit dirty I admit ) but it works.

  1. Extract the contents of the jar in a directory and decompile it like you done it before
  2. Next, make an empty project inside netbeans ( with no src directory or anything )
  3. Copy the contents of your decompiled project into this netbeans folder. You should now see a skeleton structure of some files. Note The poject is inisde netbeans but it is not a java project yet.
  4. Edit the classpath of the netbeans project and add java library to make it into a Java netbeans project.

The other technique that I use sometimes is that I make all the project files required by the IDE (Eclipse in my case) manually and then tell eclipse to import the project. When eclipse finds all the required files ( .project, .classpath and all ), it imports into the IDE just fine

2
5/15/2014 6:19:06 AM

  1. Create a JavaFX project in your IDE.
  2. Decompile the jar to get the java files. The files will be places in different folders and sub folders based on the packaging.
  3. Put the folders as packages in the project created in step 1 above.
  4. Clean and compile the project in your IDE.

P.S: Make sure you respect the license agreements if you are planning the use the decompiled and recompiled classes in your project.


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon