Running Java program/Jar from command line

2021-01-01T05:52:28

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();

}

}

Copyright License:
Author:「y.low」,Reproduced under the CC 4.0 BY-SA copyright license with link to original source & disclaimer.
Link to:https://stackoverflow.com/questions/65525805/running-java-program-jar-from-command-line

About “Running Java program/Jar from command line” questions

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.co...
I have a simple Java file Q.java that depends on an external library file X.jar. Both Q.java and X.jar are in the same directory. I can compile Q.java from the command line by doing: "javac -cp X.j...
I've created a java program and I'm wanting to run it from the iSeries. I've been able to get it to run from the QSH so I know it compiled and runs fine, but I need to run it from the command line ...
I'm trying to write a java-program that reads from pdf-files using iText7. IText7 is a collection of several jar-files. My program (create_PDF.java) uses 3 classes - PdfWriter.class and PdfDocument.
This is the code I am running in Java project to read from a text file: ClassLoader classLoader = getClass().getClassLoader(); File file = new File(classLoader.getResource("wordsEn.txt").getFile());
I've made a command line application in Java using Eclipse. I've created a runnable JAR and understand that if I want to run it, I need to do it via command prompt. Is there any way to have the .ja...
I am running a Java program with the following command: java -cp .:./* com.bot.fix.botclient All the jar files are in the same directory. It works FINE! But what if I want to run it from a differ...
So far, I've only ran simple apps from the command line via javac filename.java. Now, I've created an application in NetBeans and I really want it to run from the command line. I am using an extern...
I am scheduling a task (with windows task scheduler) which simply run a batch file. this batch is running a jar file (Java program) with a simple "Java -jar test.jar". When i run the script from ...
Ok I know this question has been asked many many many times before, but I've googled it and looked at examples and looked at questions on SO for the past month, and I seriously cannot get this to w...

Copyright License:Reproduced under the CC 4.0 BY-SA copyright license with link to original source & disclaimer.