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

java.lang.ClassNotFoundException during GrammarInitializer instantiation #20

Closed
gilcesarf opened this issue Apr 22, 2020 · 2 comments
Closed

Comments

@gilcesarf
Copy link
Contributor

I have created a new scenario and configured a GrammarInitializer implemented inside my src/test/java root. A ClassNotFound exception was raised as at the end.

I investigated the problem and it is caused because of this snippet inside com.khubla.antlr.antlr4test.Scenario class:

public ClassLoader getClassLoader() throws MalformedURLException, ClassNotFoundException {
    final URL antlrGeneratedURL = new File(baseDir + "/target/classes").toURI().toURL();
    final URL[] urls = new URL[] { antlrGeneratedURL };
    return new URLClassLoader(urls, Thread.currentThread().getContextClassLoader());
}

This is the code used to get a classloader to load the Lexer and Parser. It points explicitly to "/target/classes". But when the GrammarInitializer is put inside /src/test/java, the corresponding .class gets compiled to "/target/test-classes", so it cannot be found.

Until I fix this, a possible workaround is to put the GrammarInitializer implementation inside the main source project root: src/main/java

This workaround solved the ClassNotFoundException. Its drawback is that GrammarInitializer class would be distributed with the .jar of the project.

java.lang.ClassNotFoundException: io.trustep.grammars.mysql.SmokeInitializer
	at java.base/java.net.URLClassLoader.findClass(URLClassLoader.java:466)
	at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:563)
	at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:496)
	at com.khubla.antlr.antlr4test.ScenarioExecutor.testGrammar(ScenarioExecutor.java:112)
	at com.khubla.antlr.antlr4test.ScenarioExecutor.testGrammars(ScenarioExecutor.java:72)
	at com.khubla.antlr.antlr4test.GrammarTestMojo.testScenarios(GrammarTestMojo.java:294)
	at com.khubla.antlr.antlr4test.GrammarTestMojo.execute(GrammarTestMojo.java:169)
	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 java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base/java.lang.reflect.Method.invoke(Method.java:564)
	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)
@gilcesarf
Copy link
Contributor Author

The initial idea I have to fix the problem is to add a parameter to the method getClassLoader() and pass "/target/classes" or "/target/test-classes" if instantiating a Lexer/Parser or a GrammarInitializer. Will take a closer look at the code to check if its a good approach.

gilcesarf added a commit to gilcesarf/antlr4test-maven-plugin that referenced this issue Apr 22, 2020
gilcesarf added a commit to gilcesarf/antlr4test-maven-plugin that referenced this issue Apr 22, 2020
gilcesarf added a commit to gilcesarf/antlr4test-maven-plugin that referenced this issue Apr 22, 2020
@gilcesarf
Copy link
Contributor Author

Already fixed the issue. Waiting on PR to close issue

teverett added a commit that referenced this issue Apr 23, 2020
teverett added a commit that referenced this issue May 30, 2021
…n.plugins-maven-javadoc-plugin-3.3.0

Bump maven-javadoc-plugin from 3.2.0 to 3.3.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant