简体   繁体   中英

Running "java ivy.IVYbot.IVYbot.Main" in target/classes directory returns cannot find main class error

I'm trying to use the Windows command line to run my class files. However, when I try to run it, nothing ever works and I only see the

Error: Could not find or load main class Main.

I've tried several different solutions all around this site. (The class file I want is located in target/classes/ivy/IVYbot/IVYbot/Main.class.)

> C:\Users\Ivy\Documents\GitHub\IVYbot\target\classes> java ivy.IVYbot.IVYbot.Main
Error: Could not find or load main class ivy.IVYbot.IVYbot.Main

> C:\Users\Ivy\Documents\Github\IVYbot> java -cp .;.\target\classes Main
Error: Could not find or load main class Main.

> C:\Users\Ivy\Documents\Github\IVYbot> java -cp .;.\target\classes ivy.IVYbot.IVYbot.Main
Error: Could not find or load main class ivy.IVYbot.IVYbot.Main.

> C:\Users\Ivy\Documents\Github\IVYbot\target\classes\ivy\IVYbot\IVYbot> java Main
Error: Could not find or load main class Main.

I even tried compiling the entire thing into a .jar file through Maven (with dependencies).

> C:\Users\Ivy\Documents\Github\IVYbot\target> java -jar IVYbot-0.0.1-SNAPSHOT-jar-with-dependencies.jar
no main manifest attribute, in IVYbot-0.0.1-SNAPSHOT-jar-with-dependencies.jar

though it should, as

> C:\Users\Ivy\Documents\Github\IVYbot\target> jar tf IVYbot-0.0.1-SNAPSHOT-jar-with-dependencies.jar
META-INF/
META-INF/MANIFEST.MF
...

shows. I have no idea what's wrong with my pathing. The plugin that I'm using for my Maven, if that's useful:

<build>
    <plugins>
        <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>single</goal>
            </goals>
          </execution>
        </executions>
        <configuration>
          <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
          </descriptorRefs>
        </configuration>
      </plugin>
    </plugins>
</build>

After you have compiled with the maven plugin and assembled the jar with its dependency, you can run your class using the following command:

java -cp IVYbot-0.0.1-SNAPSHOT-jar-with-dependencies.jar ivy.IVYbot.IVYbot.Main

Otherwise, if you want to run only the class that you have compiled you can use the following command, but remember to be in the directory where the compiled class is:

java -cp . Main

This because you have to specify the classpath in order to execute your class and the . tells java to use the current directory as classpath.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM