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
------------------------------------------------------------------------
matt
  • 10,892
  • 3
  • 22
  • 34
Kanishka
  • 115
  • 2
  • 8
  • How did you run the application? – matt May 16 '23 at 07:16
  • I used Netbeans as the IDE. Using Run command after right clicking on the project – Kanishka May 16 '23 at 07:30
  • 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. – matt May 16 '23 at 07:32
  • This issue won't occure in Maven Java application. only in FXML JavaFX Maven Archetype – Kanishka May 16 '23 at 10:15
  • Are you using the `javafx-maven-plugin`? Also check your `nbactions.xml`, and verify that you didn't inadvertently do t[this](https://stackoverflow.com/a/73611827/230513). – trashgod May 16 '23 at 15:36
  • Yes, I'm using javafx-maven-plugin @trashgod – Kanishka May 17 '23 at 06:19
  • You might review the `module-info.java` for both projects. – trashgod May 17 '23 at 11:49
  • 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. – jewelsea May 17 '23 at 22:52
  • @jewelsea: I proposed a working example below; I'd welcome any insight. – trashgod May 17 '23 at 23:59
  • 1
    @trashgod 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](https://stackoverflow.com/questions/43192741/how-should-i-name-my-java-9-module), same for the artifact id. – jewelsea May 18 '23 at 04:27

1 Answers1

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.

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • 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. – Kanishka May 18 '23 at 10:11
  • Glad you found a way forward. I pursued a similar course on my sole remaining Java 8 legacy project, while using either [tag:ant] or [tag:maven] projects for newer work. – trashgod May 18 '23 at 12:20