Error own Maven Dependency to new FXML project
Asked Answered
L

1

2

I have created simple maven project for two numbers to sum.

public class AddNum {
    public int getTwoNumbers(int x, int y) {
        int addnum = x + y;
        return addnum;
    }
}

After that I used mvn clean install and it created me jar file in my local maven repositary. Then i added this to a new FXML JavaFX Maven Archetype(Gluon) project as a dependency to the .pom as show below.

<dependencies>
    <dependency>
        <groupId>com</groupId>
        <artifactId>mavenproject1</artifactId>
        <version>2.0-SNAPSHOT</version>
    </dependency>
</dependencies>

Then init the added depenedency as follows.

    public static void main(String[] args) {
        AddNum a = new AddNum();
        System.out.println(a.getTwoNumbers(5, 10));
        launch();
    }

But when I run the project it gives me below error.

java.lang.reflect.InvocationTargetException
    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:497)
    at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
    at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
    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:497)
    at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.NoClassDefFoundError: newpackage/AddNum
    at com.mycompany.testfx.App.main(App.java:36)
    ... 11 more
Caused by: java.lang.ClassNotFoundException: newpackage.AddNum
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 12 more
Exception running application com.mycompany.testfx.App
Command execution failed.
org.apache.commons.exec.ExecuteException: Process exited with an error: 1 (Exit value: 1)
    at org.apache.commons.exec.DefaultExecutor.executeInternal (DefaultExecutor.java:404)
    at org.apache.commons.exec.DefaultExecutor.execute (DefaultExecutor.java:166)
    at org.openjfx.JavaFXBaseMojo.executeCommandLine (JavaFXBaseMojo.java:504)
    at org.openjfx.JavaFXBaseMojo.executeCommandLine (JavaFXBaseMojo.java:394)
    at org.openjfx.JavaFXRunMojo.execute (JavaFXRunMojo.java:100)
    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:497)
    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)
org.apache.commons.exec.ExecuteException: Process exited with an error: 1 (Exit value: 1)
    at org.apache.commons.exec.DefaultExecutor.executeInternal(DefaultExecutor.java:404)
    at org.apache.commons.exec.DefaultExecutor.execute(DefaultExecutor.java:166)
    at org.openjfx.JavaFXBaseMojo.executeCommandLine(JavaFXBaseMojo.java:504)
    at org.openjfx.JavaFXBaseMojo.executeCommandLine(JavaFXBaseMojo.java:394)
    at org.openjfx.JavaFXRunMojo.execute(JavaFXRunMojo.java:100)
    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:497)
    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)
------------------------------------------------------------------------
BUILD FAILURE
------------------------------------------------------------------------
Lauder answered 16/5, 2023 at 6:55 Comment(10)
How did you run the application?Fiddlehead
I used Netbeans as the IDE. Using Run command after right clicking on the projectLauder
Odd, but netbeans compiles the project fine? It sounds like a case of having netbeans reload the project pom.xml file. I'm surprised that it recognizes the new class in the editor.Fiddlehead
This issue won't occure in Maven Java application. only in FXML JavaFX Maven ArchetypeLauder
Are you using the javafx-maven-plugin? Also check your nbactions.xml, and verify that you didn't inadvertently do tthis.Vow
Yes, I'm using javafx-maven-plugin @VowLauder
You might review the module-info.java for both projects.Vow
So the error is this java.lang.ClassNotFoundException: newpackage.AddNum. There are answers on StackOverflow that address that, but most refer to the classpath and predate the module system. Your issue could be that the class with that name in that package does not exist. Or it could be that the module it is in is not accessible from your access point. What it is, I could not say.Monmouth
@jewelsea: I proposed a working example below; I'd welcome any insight.Vow
@Vow your approach looks right to me. The only thing I'd suggest adding to the answer is the module-info.java that was part of the archetype. And a note that the module name org.openjfx is just because it came from the openjfx sample code. When adapting for their own code, developers should be replace the module name with their own module name following the recommended naming strategy, same for the artifact id.Monmouth
V
3

The crucial step seems to be ensuring that the client project requires the dependency in module-info.java.

Using the current javafx-maven-archetypes, I created a simple project named "sample" as shown here and installed the artifact.

mvn -B archetype:generate \
    -DarchetypeGroupId=org.openjfx \
    -DarchetypeArtifactId=javafx-archetype-simple \
    -DarchetypeVersion=0.0.6 \
    -DgroupId=org.openjfx \
    -DartifactId=sample \
    -Dversion=1.0.0 \
    -Djavafx-version=17.0.7
pushd sample/
mvn javafx:run
mvn install

I then cloned this project and added the corresponding dependency in pom.xml:

<dependency>
    <groupId>org.openjfx</groupId>
    <artifactId>sample</artifactId>
    <version>1.0.0</version>
</dependency>

I altered the project's module-info.java so that it requires the new dependency:

requires org.openjfx;

Finally, I modified ModularApp to import the dependency and invoke a method:

import org.openjfx.SystemInfo;
…
@Override
public void start(Stage stage) throws IOException {
    …
    System.out.println(SystemInfo.javafxVersion());
}

Console:

mvn javafx:run
17.0.7

Addendum: For reference, you can see the prototypes used by the archetype here. As @jewelsea comments, you will likely want to edit the generated pom.xml and module-info.java to follow the recommended naming strategy.

Vow answered 17/5, 2023 at 23:53 Comment(2)
I'm working with Java 8 so module-info.java not working with it. But what you are saying is working, but I finally decide to create a simple maven project without going for FXML JavaFX Maven Archetype(Gluon) project and add javafx dependency and conitune working with it because it wont give my original error when adding own dependencies.Lauder
Glad you found a way forward. I pursued a similar course on my sole remaining Java 8 legacy project, while using either ant or maven projects for newer work.Vow

© 2022 - 2024 — McMap. All rights reserved.