Proguard混淆后HelloWorld示例程序无法运行问题咨询
Hey there, let's work through this issue you're facing—obfuscating your JAR with ProGuard leads to a "main method not found" error, and even decompiling doesn't show the main method anymore. Here's what's happening and how to fix it:
Why This Happens
ProGuard's default behavior is to remove code it deems "unused" and obfuscate class/method names. Since it doesn't automatically recognize your main method as the application entry point, it might either rename it beyond recognition or strip it entirely during optimization.
Step-by-Step Fixes
Let's adjust your ProGuard configuration to preserve the necessary components:
Explicitly keep the main method and its containing class
Add these rules to your ProGuard config file—this tells ProGuard not to touch the main class or its main method:# Keep the HelloWorld class and its main method -keep class com.proguard.sample.HelloWorld { public static void main(java.lang.String[]); }If you want to keep the entire class's name and structure intact (to avoid messing with your JAR's MANIFEST.MF), you can simplify it to:
-keep class com.proguard.sample.HelloWorldVerify your MANIFEST.MF (if applicable)
If your original JAR has a MANIFEST.MF that specifiesMain-Class: com.proguard.sample.HelloWorld, make sure the obfuscated JAR still includes this manifest, and that the class name hasn't been changed (the rule above ensures this).Check for over-aggressive optimizations
If you've added-optimizationsflags, ensure none of them are configured to remove entry-point methods. For most simple cases, sticking with the default optimization settings plus the keep rules above will work.
How to Test
- Re-run ProGuard with the updated configuration to generate the obfuscated JAR
- Execute the new JAR—you should see "Hello world" print as expected
- Decompile the obfuscated class file again; the
mainmethod should now be present (either with its original name or preserved if you kept the class intact)
内容的提问来源于stack exchange,提问作者Pej

