Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NoClassDefFoundError when including gcc as library #3934

Closed
codemonstur opened this issue Apr 14, 2022 · 4 comments
Closed

NoClassDefFoundError when including gcc as library #3934

codemonstur opened this issue Apr 14, 2022 · 4 comments
Assignees
Labels
triage-done Has been reviewed by someone on triage rotation.

Comments

@codemonstur
Copy link

I've written a tool that adds gcc as a library in my tools fatjar. When running this tool I get NoClassDefFoundError's.

Initially the error was for a missing LimitInputStream. Which Google said is part of an old version of Guava. Looking at the pom on MavenCentral I saw no dependencies there. And to be absolutely sure I ran mvn dependency:tree and found no transitive dependencies listed.

Looking through the bazel build I found a list of dependencies:

   deps = [
        "//src/com/google/debugging/sourcemap/proto:mapping_java_proto",
        "//src/com/google/javascript/jscomp/conformance:conformance_java_proto",
        "//src/com/google/javascript/jscomp/instrumentation/reporter/proto:profile_java_proto",
        "//src/com/google/javascript/rhino/typed_ast:typed_ast_java_proto",
        "@args4j_args4j",
        "@com_google_code_gson_gson",
        "@com_google_guava_failureaccess//jar",
        "@com_google_guava_guava//jar",
        "@com_google_protobuf//:protobuf_java",
        "@com_google_re2j_re2j",
        "@google_bazel_common//third_party/java/auto:value",
        "@google_bazel_common//third_party/java/error_prone:annotations",
        "@google_bazel_common//third_party/java/jsr305_annotations",
        "@org_apache_ant_ant",
    ],

LimitInputStream however is not in the jar:

$ unzip -l closure-compiler-v20220405.jar | grep LimitInputStream
$

I don't see version numbers in the bazel build so I don't know what I am supposed to add. I gave it a good guess for Guava, and for good measure I also added failureaccess.

com.google.guava:failureaccess:1.0.1
com.google.guava:guava:14.0

Now it looks like it can find LimitInputStream, but dies with a new NoClassDefFoundError for JSSourceFile.

Exception in thread "main" java.lang.NoClassDefFoundError: com/google/javascript/jscomp/JSSourceFile

I checked, it is not in the jar: $ unzip -l closure-compiler-v20220405.jar | grep JSSourceFile. But it seems to be part of the closure compiler source code so I have no idea what to add now.

How can I add gcc as a library and have everything I need to actually run it? FYI I'm trying to use the JS compressor.

@niloc132
Copy link
Contributor

niloc132 commented Apr 14, 2022

I've been working on #3896 recently, and just pushed a pull request to try to fix this, but your specific issue wasn't on my radar when I was doing that.

I just checked the generated copy of the unshaded jar that this patch will produce, and it includes

        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>31.0.1-jre</version>
        </dependency>

which should satisfy what you're after?

I'm not sure if this will solve your JSSourceFile issue though - it looks like that file was removed in ae5f1d7e6 - is it possible you have more than one copy of closure-compiler on your classpath, and so are getting conflicts from other classes?

@codemonstur
Copy link
Author

I wrote an SSCCE to try and clarify my issue. And I found both the problem and a solution.

I was using com.googlecode.htmlcompressor:htmlcompressor:1.5.2 to use GCC. It is the latest version but it also dates back to 2011. So not compatible anymore I guess. I found another project com.github.hazendaz:htmlcompressor:1.7.3 which was last updated this January and it appears to work.

It is a little unclear to me which library is responsible for what work. The ClosureJavaScriptCompressor class is in the htmlcompressor library but the CompilationLevel enum is in gcc. Is it possible to use the JS compression with just one of these libraries?

@rishipal
Copy link
Contributor

The com.googlecode.htmlcompressor:htmlcompressor:1.5.2 is not updated since many years. It uses the ClosureJavaScriptCompressor - a basic JS compression implementation which uses closure compiler. But that closure compiler compiler is the release version r1043 which is very old - https://github.com/marconilanna/htmlcompressor/blob/e67a47fd7ac540285a81281612946be5115377bd/pom.xml#L25
The JSSourceFile usages were deleted subsequently.

The com.github.hazendaz:htmlcompressor:1.7.3 uses the latest release v20220405 - https://github.com/hazendaz/htmlcompressor/blob/fb1157513faedb8bf584aa913b15cb7e029f0024/pom.xml#L118.

I'm unfamiliar how htmlcompression works to truly tell the differences between the two.

@rishipal rishipal added the triage-done Has been reviewed by someone on triage rotation. label Apr 22, 2022
@codemonstur
Copy link
Author

I rummaged through the source code of htmlcompressor to see what it actually did. The ClosureJavaScriptCompressor does some plumbing and then finally calls this code:

Compiler compiler = new Compiler();
compiler.disableThreads();
...
Result result = compiler.compile(externsList, input, compilerOptions);

All of that is inside GCC, so I could just copy that and delete the htmlcompressor dependency. I happen to need this library for other stuff so for now I'll keep it.

The NoClassDefFoundError is really just caused by bad dependency management. The googlecode version of htmlcompressor doesn't explicitly list which version of GCC it can handle and doesn't include it by default. As far as I'm concerned you can close this issue.

@rishipal rishipal self-assigned this Apr 26, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
triage-done Has been reviewed by someone on triage rotation.
Projects
None yet
Development

No branches or pull requests

3 participants