简体   繁体   中英

Running Java program/Jar from command line

I have a java program that I coded in intellij using maven. I exported it as a jar and tried running from the command line but it keeps on telling me

Error: Could not find or load main class com.company.Main Caused by: java.lang.ClassNotFoundException: com.company.Main

I then attempted to run the program itself from the command line and that also tells me it cannot find the main class

command line: for the jar: java -jar selenium_project.jar

for running the program:java com.company.Main

For the jar I am running it from the folder it is in. For the program I am running it from the root of the program.

Pom.xml

<build>
<plugins>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <version>2.4</version>
        <configuration>
            <archive>
                <index>true</index>
                <manifest>
                    <mainClass>com.company.Main</mainClass>
                </manifest>
            </archive>
        </configuration>
    </plugin>
</plugins></build>

Manifest:

Manifest-Version: 1.0
Main-Class: com.company.Main

It might be an issue with maven, I dont know. I created a simple hello world program and ran that from the command line and it worked. I ran it like this (c://etc)../src/> java com.company.HelloWorld. But again when i try for this one it cannot find the main class. Thank you

These are my classes

EDIT: I changed to use the maven-assembly-plugin and I rebuilt the jar and still does not work

<build>

<plugins>

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-assembly-plugin</artifactId>
        <executions>
            <execution>
                <phase>package</phase>
                <goals>
                    <goal>single</goal>
                </goals>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>
                                com.company.Main
                            </mainClass>
                        </manifest>
                    </archive>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
            </execution>
        </executions>
    </plugin>

</plugins></build>

class structure to show where main class is

code in the Main class

public class Main {

public static void main(String[] args) throws IOException, SQLException {
    System.out.println("hello");

    Executor ex = new Executor();

}

}

Your error is probably that your Main class is in the wrong package.

Furthermore, please not that you need the Maven shade plugin or Maven assembly plugin to build executable jars if you have dependencies.

I dont know what the issue was. I tried creating a new program and i copied over the classes and remade the pom.xml file and my new program works except for one dependency (chrome options) but I dont think that is what was causing an issue. So now I just need to figure out why Chrome options wont work but the actual jar from the new program does run.

From: https://www.xspdf.com/resolution/54971948.html To extract the files from a jar file, use x, as in: C:\Java> jar xf myFile.jar. To extract only certain files from a jar file, supply their Install an archive program. JAR files work just like ZIP files. You can use any archive program to extract them. 2. Find the JAR file you want to extract. Use File Explorer (press Win + E to open File Explorer) or Finder on Mac to 3. Right-click the JAR file. Doing so. How to extract manifest file from jar. Extracting the Contents of a JAR File.

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