0

I am an absolute beginner (just started learning Java/programming) and I am using NetBeans IDE 13 with JDK 15.0.1 for school

I'm having this issue when I run this simple Java Hello World program in Apache NetBeans IDE 13:

/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Project/Maven2/JavaApp/src/main/java/${packagePath}/${mainClassName}.java to edit this template
 */

package com.mycompany.testing;

/**
 *
 * @author Lenovo
 */
public class Testing {

    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

The pom.xml :

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.mycompany.testing</groupId>
    <artifactId>Testing</artifactId>
    <version>1.0-SNAPSHOT</version>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>15</maven.compiler.source>
        <maven.compiler.target>15</maven.compiler.target>
        <exec.mainClass>com.mycompany.testing.Testing</exec.mainClass>
    </properties>
</project>

The output:

cd E:\UUM 2023 SEM6\STA1123-PROGRAMMING 2\Exercises╱Tutorials\Exercise Answers\Source Code\Testing; "JAVA_HOME=C:\\Program Files\\Java\\jdk-15.0.1" cmd /c "\"C:\\Program Files\\NetBeans-13\\netbeans\\java\\maven\\bin\\mvn.cmd\" -Dexec.vmArgs= \"-Dexec.args=${exec.vmArgs} -classpath %classpath ${exec.mainClass} ${exec.appArgs}\" \"-Dexec.executable=C:\\Program Files\\Java\\jdk-15.0.1\\bin\\java.exe\" -Dexec.mainClass=com.mycompany.testing.Testing -Dexec.classpathScope=runtime -Dexec.appArgs= \"-Dmaven.ext.class.path=C:\\Program Files\\NetBeans-13\\netbeans\\java\\maven-nblib\\netbeans-eventspy.jar\" -Dfile.encoding=UTF-8 org.codehaus.mojo:exec-maven-plugin:3.0.0:exec"
Running NetBeans Compile On Save execution. Phase execution is skipped and output directories of dependency projects (with Compile on Save turned on) will be used instead of their jar artifacts.
Scanning for projects...

-------------------< com.mycompany.testing:Testing >--------------------
Building Testing 1.0-SNAPSHOT
--------------------------------[ jar ]---------------------------------

--- exec-maven-plugin:3.0.0:exec (default-cli) @ Testing ---
Error: Could not find or load main class com.mycompany.testing.Testing
Caused by: java.lang.ClassNotFoundException: com.mycompany.testing.Testing
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.codehaus.mojo.exec.ExecMojo.executeCommandLine (ExecMojo.java:982)
    at org.codehaus.mojo.exec.ExecMojo.executeCommandLine (ExecMojo.java:929)
    at org.codehaus.mojo.exec.ExecMojo.execute (ExecMojo.java:457)
    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:972)
    at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:293)
    at org.apache.maven.cli.MavenCli.main (MavenCli.java:196)
    at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
    at jdk.internal.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:64)
    at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
    at 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)
------------------------------------------------------------------------
BUILD FAILURE
------------------------------------------------------------------------
Total time:  2.558 s
Finished at: 2023-05-07T15:41:19+08:00
------------------------------------------------------------------------
Failed to execute goal org.codehaus.mojo:exec-maven-plugin:3.0.0:exec (default-cli) on project Testing: Command execution failed.: Process exited with an error: 1 (Exit value: 1) -> [Help 1]

To see the full stack trace of the errors, re-run Maven with the -e switch.
Re-run Maven using the -X switch to enable full debug logging.

For more information about the errors and possible solutions, please read the following articles:
[Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

I need help on how to solve the error:

Error: Could not find or load main class com.mycompany.testing.Testing
Caused by: java.lang.ClassNotFoundException: com.mycompany.testing.Testing
Command execution failed.

and this problem:

Failed to execute goal org.codehaus.mojo:exec-maven-plugin:3.0.0:exec (default-cli) on project Testing: Command execution failed.: Process exited with an error: 1 (Exit value: 1) -> [Help 1]

To see the full stack trace of the errors, re-run Maven with the -e switch.
Re-run Maven using the -X switch to enable full debug logging.

For more information about the errors and possible solutions, please read the following articles:
[Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

There's no error in the syntax. All I did was run file. I just want to be able to run the code and have the IDE run like usual.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • Can you show the configuration for the exec-maven-plugin and your pom file? Which Maven verison do you use ? – khmarbaise May 07 '23 at 09:11
  • @khmarbaise I added the pom.xml file above. How do I check the exec-maven-plugin and which maven version I'm using currently? Thank you – Amir Haziq May 07 '23 at 09:52
  • Does this answer your question? [How do I execute a program using Maven?](https://stackoverflow.com/questions/2472376/how-do-i-execute-a-program-using-maven) – andrewJames May 07 '23 at 12:46
  • Add the `exec-maven-plugin` to your POM. See also [Run a Java Main Method in Maven](https://www.baeldung.com/maven-java-main-method). Another place to set the main method is in the NetBeans IDE: go to File > Project Properties > Run > Main Class. Click on the "Browse" button. – andrewJames May 07 '23 at 12:50
  • @AmirHaziq [1] To determine your Maven version in NetBeans: navigate to **Tools > Options >Java > Maven > Execution**, then look directly under the **Maven Home** field. [2] Your output shows the exec-maven-plugin version is 3.0.0. [3] I can build and run a trivial Maven _"Hello World!"_ application in NetBeans 17 without any problems, and my **pom.xml** is almost identical to yours. So while it is a workaround rather than a solution to your specific problem, consider using [NetBeans 17 instead](https://netbeans.apache.org/download/nb17/) if possible. It uses exec-maven-plugin version 3.1.0. – skomisa May 07 '23 at 22:15

0 Answers0