0

I am trying to follow the Maven in 5 Minutes tutorial for my project but for some reason I can't seem to actually run my project when it builds. I have the following pom.xml file:

<?xml version="1.0"?>
<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.my-group</groupId>
<artifactId>my-artifact</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
    <dependency>
        <groupId>com.sun</groupId>
        <artifactId>tools</artifactId>
        <version>1.8.0</version>
        <scope>system</scope>
        <systemPath>${java.home}/../lib/tools.jar</systemPath>
    </dependency>
    <dependency>
        <groupId>commons-cli</groupId>
        <artifactId>commons-cli</artifactId>
        <version>1.3.1</version>
    </dependency>
    <dependency>
        <groupId>com.oracle</groupId>
        <artifactId>javafx</artifactId>
        <version>2.2.3</version>
        <scope>system</scope>
        <systemPath>${project.basedir}/jfxrt.jar</systemPath>
    </dependency>
</dependencies>
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.5.1</version>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
                <manifest>
                    <mainClass>com.my.package.MyClass</mainClass>
                </manifest>
            </configuration>
        </plugin>
    </plugins>
</build>
</project>

The tutorial seems to imply that I can do this from my project root:

mvn package
java -cp target/my-artifact-0.0.1-SNAPSHOT.jar com.my.package.MyClass

However, if I try to do this, I get the following error message:

Error: Could not find or load main class com.my.package.MyClass

If I run java -cp ../my-artifact-0.0.1-SNAPSHOT.jar com.my.package.MyClass from the target/classes directory I still get the same message, which is weird because I've verified that the .class file is there. Why am I not able to run my project after building it with the above steps?

Byte Lab
  • 1,576
  • 2
  • 17
  • 42
  • My advice: don't use outdated software and maven system dependencies. Instead use the [JavaFX maven plugin](https://github.com/javafx-maven-plugin/javafx-maven-plugin) with [Java 8+](http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html). – jewelsea Jul 19 '16 at 00:38
  • @jewelsea I believe we have to use maven system dependencies for tools.jar. Thanks for linking the JavaFX maven plugin though, I'll take a look. – Byte Lab Jul 19 '16 at 00:40
  • System dependencies are only required for use of outdated JavaFX installations for Java 7 environments, not more modern Java 8 environments: see [Compile JavaFX manually](http://stackoverflow.com/questions/9436219/compile-code-using-javafx-2-0-using-command-line). – jewelsea Jul 19 '16 at 00:43
  • Your should confirm your Java environment,and refer to http://stackoverflow.com/questions/7485670/error-could-not-find-or-load-main-class – 陈志明 Jul 19 '16 at 00:45
  • Also use `-jar` to run a jar, not `-cp`. You also need to have the [main class specified in the jar manifest](https://docs.oracle.com/javase/tutorial/deployment/jar/appman.html) rather than at the command line. Note: if you use the JavaFX maven plugin, it will handle setting the manifest appropriately for you. – jewelsea Jul 19 '16 at 00:45
  • You are correct that for `tools.jar`, as opposed to JavaFX under Java 8, you do need to have it as a [system dependency](https://maven.apache.org/general.html#tools-jar-dependency), if you wish to use it. Note: in doing so, you can only [run the resultant application on a machine with the JDK installed, but not on a machine with only the JRE](http://stackoverflow.com/questions/5730815/unable-to-locate-tools-jar). Also, the application likely will not work with Java 9+ due to tools.jar being `com.sun`. – jewelsea Jul 19 '16 at 00:51
  • @jewelsea - Thank you, but do you know why I wouldn't be able to run without the manifest tag? I'm aware that I would be able to simply run `java -jar snapshot.jar` if I did, but I'd still like to know why I can't run my program using the approach specified in the maven tutorial. – Byte Lab Jul 19 '16 at 01:06
  • @陈志明 That post seems to suggest that what I'm doing is correct -- I'm including the jar file in my classpath and giving the fully qualified name to the class I want to run. – Byte Lab Jul 19 '16 at 01:07
  • If you wish to use the cp approach and you are not using Java 8, then you will need to add the jfxrt.jar file to the `-cp` path, and possibly also the `tools.jar` file as well. Though either of those would not necessarily result in could not find or load for your Main class. Other reasons that can result in the main class not loading is if it is not declared public, it does not have a `public static void main` method (and/or public start method for JavaFX), it includes a static initialization block that fails, it does not have the correct package definition in the file for its location, etc – jewelsea Jul 19 '16 at 01:22
  • @jewelsea You were right that using the plugin made it easy. Still though, I'd be curious to know why I can't just use the classes output in the jar with `-cp`. – Byte Lab Jul 19 '16 at 02:23

0 Answers0