proguard-maven-plugin icon indicating copy to clipboard operation
proguard-maven-plugin copied to clipboard

Unable to work with 7.x - ClassNotFoundException on ClassVisitor

Open xmariachi opened this issue 2 years ago • 4 comments

Hi, I have been trying a lot of things and had different issues on each case, not being able to perform a single obfuscation.

I have a JDK8 application(compiled against and expected to be run on JDK8), but apparently some of the libraries to obfuscate are from a higher version, up to 15. If I use older net.sf.proguard versions (<7) I get an Unsupported major-minor version problem, which should be fixed by using newer 7.x (e.g. 7.1.1) which already covers this.

So I'm getting the following error:

[INFO] proguard jar: [C:\Users\MyUser\.m2\repository\com\guardsquare\proguard-base\7.0.0\proguard-base-7.0.0.jar]
 [proguard] Error: A JNI error has occurred, please check your installation and try again
 [proguard] Exception in thread "main" java.lang.NoClassDefFoundError: proguard/classfile/visitor/ClassVisitor
 [proguard]     at java.lang.Class.getDeclaredMethods0(Native Method)
 [proguard]     at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
 [proguard]     at java.lang.Class.privateGetMethodRecursive(Class.java:3048)
 [proguard]     at java.lang.Class.getMethod0(Class.java:3018)
 [proguard]     at java.lang.Class.getMethod(Class.java:1784)
 [proguard]     at sun.launcher.LauncherHelper.validateMainClass(LauncherHelper.java:650)
 [proguard]     at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:632)
 [proguard] Caused by: java.lang.ClassNotFoundException: proguard.classfile.visitor.ClassVisitor
 [proguard]     at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
 [proguard]     at java.lang.ClassLoader.loadClass(ClassLoader.java:418)
 [proguard]     at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:355)
 [proguard]     at java.lang.ClassLoader.loadClass(ClassLoader.java:351)
 [proguard]     ... 7 more
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE

which I've seen in another issue resolved here, but none of the solutions suggested there helped. I am trying this with jar-with-dependencies but it also happens when I run it against a single jar.

My relevant POM sections:

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.assembly.plugin.version>2.6</maven.assembly.plugin.version>
    <maven.compiler.version>3.3</maven.compiler.version>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
    <maven.release.plugin.version>2.5.3</maven.release.plugin.version>
    <java.version>1.8</java.version>
  </properties>

<build>
  <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>single</goal>
            </goals>
            <configuration>
              <descriptorRefs>
                <descriptorRef>jar-with-dependencies</descriptorRef>
              </descriptorRefs>
            </configuration>
          </execution>
        </executions>
      </plugin>

      <plugin>
        <groupId>com.github.wvengen</groupId>
        <artifactId>proguard-maven-plugin</artifactId>
        <executions>
          <execution>
            <phase>package</phase>
            <goals><goal>proguard</goal></goals>
            <configuration>
              <injar>${project.build.finalName}-jar-with-dependencies.jar</injar> <!-- make sure to obfuscate the jar with dependencies -->
              <outjar>${project.build.finalName}-small.jar</outjar>
              <proguardVersion>7.1.1</proguardVersion>
              <options>
                <option>-allowaccessmodification</option>
                <option>-dontoptimize</option>
                <option>-dontshrink</option>
                <option>-dontnote</option>
                <option>-dontwarn</option> <!-- added option to ignore com.sun missing classes -->
                <option>-keepattributes Signature</option>
                <option>-keep class mypackage.myClass { *; }</option>
              </options>
              <libs>
                <lib>${java.home}\lib\rt.jar</lib>
              </libs>
               <dependencies>
                <dependency>
                  <groupId>com.guardsquare</groupId>
                  <artifactId>proguard-base</artifactId>
                  <version>7.1.1</version>
                  <scope>runtime</scope>
                </dependency>
                 <dependency>
                   <groupId>com.guardsquare</groupId>
                   <artifactId>proguard-core</artifactId>
                   <version>7.1.1</version>
                   <scope>runtime</scope>
                 </dependency>
              </dependencies>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>

    <pluginManagement>
      <plugins>
        <plugin>
          <groupId>com.github.wvengen</groupId>
          <artifactId>proguard-maven-plugin</artifactId>
          <version>2.3.1</version>
        </plugin>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>${maven.compiler.version}</version>
          <configuration>
            <source>${java.version}</source>
            <target>${java.version}</target>
          </configuration>
        </plugin>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-assembly-plugin</artifactId>
          <version>${maven.assembly.plugin.version}</version>
          <executions>
            <execution>
              <id>jar-with-dependencies</id>
              <phase>package</phase>
              <goals>
                <goal>single</goal>
              </goals>
              <configuration>
                <descriptorRefs>
                  <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
              </configuration>
            </execution>
          </executions>
        </plugin>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-release-plugin</artifactId>
          <version>${maven.release.plugin.version}</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>

However, if I just upgrade my proguard-maven-plugin to latest 2.4.0, then I get this.

[INFO] proguard jar not found in pluginArtifacts
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE

What am I missing here?

My java version:

$ java -version
java version "1.8.0_281"
Java(TM) SE Runtime Environment (build 1.8.0_281-b09)
Java HotSpot(TM) 64-Bit Server VM (build 25.281-b09, mixed mode)

Note: I tried downloading and running the jars (proguard-base, proguard-core) but it also doesn't work:

 java -cp "/C/Users/MyUser/Downloads/proguard-base-7.1.1.jar;/C/Users/MyUser/Downloads/proguard-core-7.1.1.jar" proguard.Proguard target/my-jar-with-dependencies.jar
Error: Could not find or load main class proguard.Proguard

I downloaded these jars from mvnrepository page.

xmariachi avatar Aug 18 '21 16:08 xmariachi

About the manual command line run: To run Proguard you actually need Proguard Base and Core. The Base contains the main class and it is not in the classpath. I suspect the syntax you pass to -cp is malformed. See how the Drive letter C is there.

About the Maven execution: proguard/classfile/visitor/ClassVisitor is inside the Proguard core. So when you get java.lang.NoClassDefFoundError: proguard/classfile/visitor/ClassVisitor it seems that you have Proguard Base in your classpath but not the Core. Hard to say why exactly this happens. I have personally only used the newer versions with Java 11+. You could test what happens when leaving the Proguard dependency definitions completely out since it would then use 7.0.0 for Plugin version 2.3.1 and 7.1.0-beta3 for 2.4.0

If you set info log level, you should see the statement that logs the found Proguard JARs: https://github.com/wvengen/proguard-maven-plugin/blob/master/src/main/java/com/github/wvengen/maven/proguard/ProGuardMojo.java#L986

One thing to try is using the temp file for config: https://github.com/wvengen/proguard-maven-plugin/blob/master/src/main/java/com/github/wvengen/maven/proguard/ProGuardMojo.java#L144 After the build fails, you should find it at "${project.build.directory}/generated-proguard.conf" and you could check what kind of parameters it contains.

lasselindqvist avatar Aug 18 '21 16:08 lasselindqvist

About CLI: I was using MinGW, which autotranslates paths from Unix-like format. This works normally, even with the actual jar I am trying to obfuscate, which is expected to be run in "java -cp" fashion exactly like proguard.Proguard class. However the problem was on class name: it is "proguard.ProGuard" instead of "proguard.Proguard". So I'm now able to run it CLI.

$ java -cp "/C/Users/myuser/Downloads/proguard-base-7.1.1.jar:/C/Users/myuser/Downloads/proguard-core-7.1.1.jar" proguard.ProGuard
ProGuard, version 7.1.1
Usage: java proguard.ProGuard [options ...]

However, my primary goal is to make it work with Maven.

Run with 2.4.0, no dependencies: [INFO] proguard jar not found in pluginArtifacts

If I go to my .m2 repo, I can see those there in both proguard-base and proguard-core there for all versions mentioned here.

Run with 2.3.1, no dependencies: [INFO] proguard jar not found in pluginArtifacts

Run with 2.2.0, no dependencies:

[INFO] proguard jar: C:\Users\myuser\.m2\repository\net\sf\proguard\proguard-base\6.1.1\proguard-base-6.1.1.jar
 [proguard] ProGuard, version 6.1.1
 [proguard] Reading program jar [C:\workspace\myproject\target\myproject-0.1.18-SNAPSHOT-jar-with-dependencies.jar] (filtered)
 [proguard] Error: Can't read [C:\workspace\myproject\target\myproject-0.1.18-SNAPSHOT-jar-with-dependencies.jar(;;;;;;;!META-INF/maven/**)] (Can't process class [META-INF/versions/15/org/bouncycastle/jcajce/provider/asymmetric/edec/SignatureSpi$EdDSA.class] (Unsupported version number [59.0] (maximum 57.0, Java 13)))

Logs with -X from 2.4.0 no dependencies:

[DEBUG] pluginArtifact: C:\Users\myuser\.m2\repository\com\github\wvengen\proguard-maven-plugin\2.4.0\proguard-maven-plugin-2.4.0.jar
[DEBUG] pluginArtifact: C:\Users\myuser\.m2\repository\com\guardsquare\proguard-base\7.1.0-beta3\proguard-base-7.1.0-beta3.jar
[DEBUG] proguard DependencyTrail: 2
[DEBUG] pluginArtifact: C:\Users\myuser\.m2\repository\com\google\code\gson\gson\2.8.5\gson-2.8.5.jar
[DEBUG] pluginArtifact: C:\Users\myuser\.m2\repository\com\guardsquare\proguard-core\7.1.0-beta3\proguard-core-7.1.0-beta3.jar
[DEBUG] proguard DependencyTrail: 2
[DEBUG] pluginArtifact: C:\Users\myuser\.m2\repository\org\jetbrains\kotlin\kotlin-stdlib\1.3.72\kotlin-stdlib-1.3.72.jar
[DEBUG] pluginArtifact: C:\Users\myuser\.m2\repository\org\jetbrains\annotations\13.0\annotations-13.0.jar
[DEBUG] pluginArtifact: C:\Users\myuser\.m2\repository\org\jetbrains\kotlin\kotlin-stdlib-common\1.3.72\kotlin-stdlib-common-1.3.72.jar
[DEBUG] pluginArtifact: C:\Users\myuser\.m2\repository\org\jetbrains\kotlinx\kotlinx-metadata-jvm\0.1.0\kotlinx-metadata-jvm-0.1.0.jar
[DEBUG] pluginArtifact: C:\Users\myuser\.m2\repository\org\apache\ant\ant\1.10.9\ant-1.10.9.jar
[DEBUG] pluginArtifact: C:\Users\myuser\.m2\repository\org\apache\ant\ant-launcher\1.10.9\ant-launcher-1.10.9.jar
[DEBUG] pluginArtifact: C:\Java\jdk1.8.0_281\jre\..\lib\tools.jar
[DEBUG] pluginArtifact: C:\Users\myuser\.m2\repository\org\apache\maven\maven-archiver\2.4\maven-archiver-2.4.jar
[DEBUG] pluginArtifact: C:\Users\myuser\.m2\repository\org\apache\maven\maven-artifact\2.0\maven-artifact-2.0.jar
[DEBUG] pluginArtifact: C:\Users\myuser\.m2\repository\org\apache\maven\maven-model\2.0\maven-model-2.0.jar
[DEBUG] pluginArtifact: C:\Users\myuser\.m2\repository\org\apache\maven\maven-project\2.0\maven-project-2.0.jar
[DEBUG] pluginArtifact: C:\Users\myuser\.m2\repository\org\apache\maven\maven-profile\2.0\maven-profile-2.0.jar
[DEBUG] pluginArtifact: C:\Users\myuser\.m2\repository\org\apache\maven\maven-artifact-manager\2.0\maven-artifact-manager-2.0.jar
[DEBUG] pluginArtifact: C:\Users\myuser\.m2\repository\org\apache\maven\maven-repository-metadata\2.0\maven-repository-metadata-2.0.jar
[DEBUG] pluginArtifact: C:\Users\myuser\.m2\repository\org\codehaus\plexus\plexus-container-default\1.0-alpha-8\plexus-container-default-1.0-alpha-8.jar
[DEBUG] pluginArtifact: C:\Users\myuser\.m2\repository\junit\junit\3.8.1\junit-3.8.1.jar
[DEBUG] pluginArtifact: C:\Users\myuser\.m2\repository\classworlds\classworlds\1.1-alpha-2\classworlds-1.1-alpha-2.jar
[DEBUG] pluginArtifact: C:\Users\myuser\.m2\repository\org\codehaus\plexus\plexus-archiver\1.0-alpha-11\plexus-archiver-1.0-alpha-11.jar
[DEBUG] pluginArtifact: C:\Users\myuser\.m2\repository\org\codehaus\plexus\plexus-component-api\1.0-alpha-15\plexus-component-api-1.0-alpha-15.jar
[DEBUG] pluginArtifact: C:\Users\myuser\.m2\repository\org\codehaus\plexus\plexus-io\1.0-alpha-3\plexus-io-1.0-alpha-3.jar
[DEBUG] pluginArtifact: C:\Users\myuser\.m2\repository\org\codehaus\plexus\plexus-utils\1.4.9\plexus-utils-1.4.9.jar
[DEBUG] pluginArtifact: C:\Users\myuser\.m2\repository\org\codehaus\plexus\plexus-interpolation\1.6\plexus-interpolation-1.6.jar
[DEBUG] pluginArtifact: C:\Users\myuser\.m2\repository\commons-io\commons-io\2.7\commons-io-2.7.jar
[DEBUG] pluginArtifact: C:\Users\myuser\.m2\repository\org\apache\maven\maven-plugin-api\3.1.1\maven-plugin-api-3.1.1.jar
[DEBUG] pluginArtifact: C:\Users\myuser\.m2\repository\org\eclipse\sisu\org.eclipse.sisu.plexus\0.0.0.M5\org.eclipse.sisu.plexus-0.0.0.M5.jar
[DEBUG] pluginArtifact: C:\Users\myuser\.m2\repository\javax\enterprise\cdi-api\1.0\cdi-api-1.0.jar
[DEBUG] pluginArtifact: C:\Users\myuser\.m2\repository\javax\annotation\jsr250-api\1.0\jsr250-api-1.0.jar
[DEBUG] pluginArtifact: C:\Users\myuser\.m2\repository\javax\inject\javax.inject\1\javax.inject-1.jar
[DEBUG] pluginArtifact: C:\Users\myuser\.m2\repository\com\google\guava\guava\10.0.1\guava-10.0.1.jar
[DEBUG] pluginArtifact: C:\Users\myuser\.m2\repository\com\google\code\findbugs\jsr305\1.3.9\jsr305-1.3.9.jar
[DEBUG] pluginArtifact: C:\Users\myuser\.m2\repository\org\sonatype\sisu\sisu-guice\3.1.0\sisu-guice-3.1.0-no_aop.jar
[DEBUG] pluginArtifact: C:\Users\myuser\.m2\repository\aopalliance\aopalliance\1.0\aopalliance-1.0.jar
[DEBUG] pluginArtifact: C:\Users\myuser\.m2\repository\org\eclipse\sisu\org.eclipse.sisu.inject\0.0.0.M5\org.eclipse.sisu.inject-0.0.0.M5.jar
[DEBUG] pluginArtifact: C:\Users\myuser\.m2\repository\org\codehaus\plexus\plexus-component-annotations\1.5.5\plexus-component-annotations-1.5.5.jar
[DEBUG] pluginArtifact: C:\Users\myuser\.m2\repository\org\codehaus\plexus\plexus-classworlds\2.4\plexus-classworlds-2.4.jar
[INFO] proguard jar not found in pluginArtifacts
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  02:21 min
[INFO] Finished at: 2021-08-19T11:23:06+02:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal com.github.wvengen:proguard-maven-plugin:2.4.0:proguard (default) on project myproject: Obfuscation failed ProGuard (proguard.ProGuard) not found in classpath -> [Help 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal com.github.wvengen:proguard-maven-plugin:2.4.0:proguard (default) on project myproject: Obfuscation failed ProGuard (proguard.ProGuard) not found in classpath
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:215)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:156)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:148)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:117)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:81)
    at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build (SingleThreadedBuilder.java:56)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:128)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:305)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:192)
    at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:105)
    at org.apache.maven.cli.MavenCli.execute (MavenCli.java:957)
    at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:289)
    at org.apache.maven.cli.MavenCli.main (MavenCli.java:193)
    at sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke (Method.java:498)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:282)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:225)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:406)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:347)
Caused by: org.apache.maven.plugin.MojoExecutionException: Obfuscation failed ProGuard (proguard.ProGuard) not found in classpath
    at com.github.wvengen.maven.proguard.ProGuardMojo.getProguardJars (ProGuardMojo.java:952)
    at com.github.wvengen.maven.proguard.ProGuardMojo.execute (ProGuardMojo.java:739)
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo (DefaultBuildPluginManager.java:137)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:210)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:156)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:148)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:117)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:81)
    at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build (SingleThreadedBuilder.java:56)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:128)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:305)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:192)
    at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:105)
    at org.apache.maven.cli.MavenCli.execute (MavenCli.java:957)
    at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:289)
    at org.apache.maven.cli.MavenCli.main (MavenCli.java:193)
    at sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke (Method.java:498)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:282)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:225)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:406)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:347)
[ERROR]

xmariachi avatar Aug 19 '21 08:08 xmariachi

I just tried not specifying any <proguardVersion> and also not specifying any on 2.4.0. It worked ( I now get an Exception in thread "main" java.lang.ExceptionInInitializerError , see below, but that's anothe thing I guess). It now gets the

[INFO] proguard jar: [C:\Users\myuser\.m2\repository\com\guardsquare\proguard-base\7.1.0-beta3\proguard-base-7.1.0-beta3.jar, C:\Users\myuser\.m2\repository\com\guardsquare\proguard-core\7.1.0-beta3\proguard-core-7.1.0-beta3.jar]
 [proguard] ProGuard, version 7.1.0-beta3

However if I try with a different version than the default there, it fails with the messages in previous posts above.

My current problem is: When I try to run the application obfuscated, I get:

 $ java -cp target/myproject-0.1.18-SNAPSHOT-small.jar mypackage.MyClass
Exception in thread "main" java.lang.ExceptionInInitializerError
        at org.a.f.a.b.<clinit>(Unknown Source)
        at org.a.f.a.e.d.<init>(Unknown Source)
        at org.a.f.a.e.d.<clinit>(Unknown Source)
        at org.a.f.a.c.<clinit>(Unknown Source)
        at mypackage.MyClass.<clinit>(Unknown Source)
Caused by: java.lang.ClassCastException: class org.a.f.a.d.n not an enum
        at java.util.EnumSet.noneOf(Unknown Source)
        at java.util.EnumSet.allOf(Unknown Source)
        at org.a.f.a.d.n.<clinit>(Unknown Source)
        ... 5 more

Any hint here? I am excluding the mypackage.MyClass (and 6 other classes with a main method too with same statement): <option>-keep class mypackage.MyClass { *; }</option>

xmariachi avatar Aug 19 '21 12:08 xmariachi

@xmariachi I had the same errors you mentioned. But I had <proguardVersion>7.1.1</proguardVersion> still configured when running with 2.4.0, no dependencies. Removing that seemed to work for me. (but full disclosure I'm also using java 11, and not java 8)

antoine-atmire avatar Aug 31 '21 12:08 antoine-atmire