如何仅从含.class文件的JAR包生成完整项目?
Got it, let's break down how to build a complete project from a JAR that only contains .class files—no source code required. Here's a practical, step-by-step guide to get you there:
First, you need to convert the compiled .class files back into readable Java source code. There are several reliable tools for this:
- JD-GUI: A graphical tool that lets you browse the JAR and export entire packages or individual classes as source files. It’s great for quick visualization and export.
- FernFlower: A command-line tool known for producing clean, accurate decompiled code. Use this command to decompile your JAR:
java -jar fernflower.jar your-input.jar output-directory/ - Procyon: Another solid command-line option that handles modern Java features (like lambdas and streams) well.
Note: Decompiled code might have minor syntax quirks (e.g., messy generics, anonymous inner class formatting) that you’ll need to tweak later—don’t worry, these are usually easy fixes.
Create a new project using your build tool of choice (Maven or Gradle) and organize the decompiled source code correctly:
- For Maven: Place decompiled .java files in
src/main/java, matching the package structure from the original JAR (e.g., a classcom.example.MyClassgoes insrc/main/java/com/example/MyClass.java). - For Gradle: Same structure—use
src/main/javafor main source code.
Make sure the package names exactly match what’s in the .class files; otherwise, you’ll run into compilation errors later.
Your original JAR likely relies on external libraries. To find and add these:
- Use
jdeps(built into the JDK) to list dependencies:
This will output the modules or JARs your code depends on.jdeps -s your-input.jar - Add these dependencies to your build file:
- For Maven: Include
<dependency>entries inpom.xmlwith the correct groupId, artifactId, and version. - For Gradle: Add
implementationlines inbuild.gradle.
- For Maven: Include
If jdeps misses something, open the JAR in JD-GUI and look for imports in the decompiled code—you can search for those libraries manually (e.g., if you see import org.apache.commons.lang.StringUtils, add the Apache Commons Lang dependency).
Now it’s time to test if everything works:
- Run a compile command:
- Maven:
mvn compile - Gradle:
./gradlew compileJava
- Maven:
- Fix any compilation errors: These usually come from decompilation quirks (e.g., missing generics bounds) or missing dependencies. Tweak the decompiled code as needed to resolve syntax issues.
- Run the main class (if you know it) to verify functionality:
(Adjust the classpath and main class name to match your project.)java -cp target/classes:lib/* com.your.package.MainClassName
Once the project compiles and runs:
- Rename obfuscated code: If the original JAR was obfuscated (e.g., using ProGuard), decompiled code will have meaningless names like
a,b,c. Rename variables, methods, and classes to reflect their purpose—this makes the code maintainable. - Add build configuration: Set up proper build plugins (e.g.,
maven-compiler-pluginto specify your Java version) and metadata in your build file. - Write tests: If you need to modify the code later, add unit tests based on the decompiled logic to ensure changes don’t break existing functionality.
- Native methods: If your JAR contains native methods (marked with
native), decompilation won’t give you the implementation. You’ll need to find the corresponding native library (.dll, .so, .dylib) or reimplement the native logic in Java if possible. - Missing resources: Check if the original JAR had non-class resources (e.g., properties files, images). Copy these into your project’s
src/main/resourcesdirectory so they’re included in the build.
内容的提问来源于stack exchange,提问作者Surya Kiran

