You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何仅从含.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:

Step 1: Decompile .class Files to Recover Source Code

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.

Step 2: Set Up a Standard Project Structure

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 class com.example.MyClass goes in src/main/java/com/example/MyClass.java).
  • For Gradle: Same structure—use src/main/java for main source code.

Make sure the package names exactly match what’s in the .class files; otherwise, you’ll run into compilation errors later.

Step 3: Resolve Dependencies

Your original JAR likely relies on external libraries. To find and add these:

  • Use jdeps (built into the JDK) to list dependencies:
    jdeps -s your-input.jar
    
    This will output the modules or JARs your code depends on.
  • Add these dependencies to your build file:
    • For Maven: Include <dependency> entries in pom.xml with the correct groupId, artifactId, and version.
    • For Gradle: Add implementation lines in build.gradle.

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

Step 4: Compile and Validate the Project

Now it’s time to test if everything works:

  • Run a compile command:
    • Maven: mvn compile
    • Gradle: ./gradlew compileJava
  • 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:
    java -cp target/classes:lib/* com.your.package.MainClassName
    
    (Adjust the classpath and main class name to match your project.)
Step 5: Polish the Project for Maintenance

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-plugin to 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.
Step 6: Handle Edge Cases
  • 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/resources directory so they’re included in the build.

内容的提问来源于stack exchange,提问作者Surya Kiran

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 08:56:39