Search code examples
javalinuxmavencommand-lineexecution

Run Java Maven project with Linux command line


I have a Java project that prints "Hello world !". It runs well under Eclipse/Windows and on a Linux server with the command:

java MyClass.java; javac MyClass

Now that I have converted the project to a Maven project it's still running fine in Eclipse, but I can't find how to run it with a Linux command. I tried many answers that I found on forums but none work for me.

Here is an example of what I tested:

mvn package install;
cd target;
java -cp myApp-0.0.1-SNAPSHOT.jar mypackage.Mylass;

This results to an error:

Error: Could not find or load main class mypackage.Mylass

So, how can I run the Maven code on Linux without generating a jar file or at least make it work with a command line?


Solution

  • i finally managed to make it work thanks to all the answers.
    here is what i did
    i moved my Main class to the outside of packages and deleted/regenerated a new POM file with

    <manifest>
       <mainClass>Myclass</mainClass>
    </manifest>
    

    but i got some dependency errors so i generated a jar file with the dependency by adding maven plugin jar-with-dependencies so here is what the build part of my POM file looks like now

    ....
    </dependencyManagement>
    <build>
    <sourceDirectory>src</sourceDirectory>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.0</version>
        <configuration>
          <source>1.7</source>
          <target>1.7</target>
        </configuration>
      </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>MyClass</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
    
            <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>3.0.0</version>
            <configuration>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
                <archive>
                    <manifest>
                        <mainClass>MyClass</mainClass>
                    </manifest>
                </archive>
            </configuration>
            <executions>
                <execution>
                    <id>make-assembly</id> <!-- this is used for inheritance merges -->
                    <phase>package</phase> <!-- bind to the packaging phase -->
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
    </build>
    

    and here is how i execute it .

    rm -rf target/ #delete old generated files
    mvn install package  # generate new jar files
    java -jar target/App-0.0.1-SNAPSHOT-jar-with-dependencies.jar  # execution 
    

    i hope this can save someone else's time
    thanks for your help