It should be quite straightforward to run your application from an IDE with some maven support (Eclipse, IntellIJ). These IDE's will take care about creating the correct classpath.

If you want to do this manually, try this:

change to the directory that contains the pom.xml execute the maven command:

mvn clean install

This will compile your project and create the jar you defined in the pom.xml file. It runs the maven phases clean and every phase up to install (compile, test, etc).

Then collect all jar files you use as dependencies (required to run your project):

mvn dependency:copy-dependencies

This executes the dependency plugin which will copy all dependencies into target/dependency.

You can then run your main method using:

cd target/
java -cp TCPPing-0.0.1-SNAPSHOT.jar:dependency TCPPing

-cp defines the classpath (all locations / jar files / folders that contain classes). TCPPing is the class your run that has a main method.

Note the : is for Linux / Mac - I think windows uses a ;.

Answer from wemu on Stack Overflow
Top answer
1 of 3
25

It should be quite straightforward to run your application from an IDE with some maven support (Eclipse, IntellIJ). These IDE's will take care about creating the correct classpath.

If you want to do this manually, try this:

change to the directory that contains the pom.xml execute the maven command:

mvn clean install

This will compile your project and create the jar you defined in the pom.xml file. It runs the maven phases clean and every phase up to install (compile, test, etc).

Then collect all jar files you use as dependencies (required to run your project):

mvn dependency:copy-dependencies

This executes the dependency plugin which will copy all dependencies into target/dependency.

You can then run your main method using:

cd target/
java -cp TCPPing-0.0.1-SNAPSHOT.jar:dependency TCPPing

-cp defines the classpath (all locations / jar files / folders that contain classes). TCPPing is the class your run that has a main method.

Note the : is for Linux / Mac - I think windows uses a ;.

2 of 3
1

Javac knows nothing about maven. Thus it will not utilize the maven pom.xml.

The value of maven is that it removes the manual work of building, testing and releasing a project.

This includes getting dependencies, and running javac with them added to the classpath of the javac command.

You can manually execute javac after maven downloads dependencies to ~/.m2/repository. However you'll need to tell javac where to find the jars. Thus is done via the classpath argument.

If you are trying to run the project after using mvn to compile it, you'll need to do this in the same folder where your .class files were placed. Thus should be /target/java or similar.

๐ŸŒ
Metamug
metamug.com โ€บ article โ€บ java โ€บ build-run-java-maven-project-command-line.html
Java - Build Run Maven Project Command Line
November 22, 2022 - Many developers prefer to setup maven project from command line use their favorite text editor to write code. Maven is a build tool and setting up a boilerplate seed project shouldn't be an issue. #generate simple java maven project mvn archetype:generate -DgroupId=com.yourcompany -DartifactId=myproject -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
๐ŸŒ
Soham Kamani
sohamkamani.com โ€บ java โ€บ cli-app-with-maven
Running a Maven Project from the Command Line (and Building Jar Files)
June 30, 2023 - This tutorial will show you how to run a basic Java Maven project on the command line (as opposed to on an IDE).
๐ŸŒ
Apache Maven
maven.apache.org โ€บ guides โ€บ getting-started โ€บ maven-in-five-minutes.html
Maven in 5 Minutes โ€“ Maven
Maven is a Java tool, so you must have Java installed in order to proceed. First, download Maven and follow the installation instructions. After that, type the following in a terminal or in a command prompt:
๐ŸŒ
Vogella
vogella.com โ€บ tutorials โ€บ ApacheMaven โ€บ article.html
Apache Maven Tutorial
To run a Maven build from the command line, you need to install the Maven command-line tool. To verify if Maven is already installed, type the following command in your terminal: ... If you see output that includes Apache Maven, you already have Maven installed.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ create-a-new-maven-project-from-command-prompt
Create a New Maven Project from Command Prompt - GeeksforGeeks
Before creating a Maven project, ensure the following prerequisites are met: Java Development Kit (JDK) Installed: Verify by running java -version in the command prompt. Maven Installed: Verify by running mvn -version in the command prompt.
Published ย  January 9, 2026
๐ŸŒ
GitHub
github.com โ€บ NishkarshRaj โ€บ Maven-Using-CMD
GitHub - NishkarshRaj/Maven-Using-CMD: Create a project in Java using Apache Maven Build tool via Command Line and use GitHub Actions to build it on remote Docker Engine.
Create a project in Java using Apache Maven Build tool via Command Line and use GitHub Actions to build it on remote Docker Engine. - NishkarshRaj/Maven-Using-CMD
Starred by 18 users
Forked by 36 users
Languages ย  Java 84.2% | Dockerfile 15.8% | Java 84.2% | Dockerfile 15.8%
Find elsewhere
๐ŸŒ
GitHub
gist.github.com โ€บ adojos โ€บ 2aaf3abeee9422e1840235636dea9c06
Maven: Create Maven Project Using Command Line #maven ยท GitHub
Create a directory where you want to create your java maven project and start a shell in that directory. For running 'generate' goal in interactive mode simply trigger the 'generate' goal without any parameters and follow the subsequent command ...
๐ŸŒ
Quora
quora.com โ€บ How-do-you-run-a-Maven-project-from-the-command-line-in-Eclipse
How to run a Maven project from the command line in Eclipse - Quora
Right click the project โ†’ Run As โ†’ Maven Build. After clicking Maven build a window appears type Goals as clean install ยท Hope this helps!!!! For more tips and tricks check out this Idea's Experimented ...
๐ŸŒ
OpenGenus
iq.opengenus.org โ€บ building-java-application-using-apache-maven-command-line
Building a basic Java Application using Apache Maven (Command Line)
August 26, 2019 - Apache Maven automatically creates a hierarchy for source code, its unit test cases and after the build, it creates a target/ folder containing the .jar or .war file which is a platform independent binary file. Our aim is to create a basic Java Project from scratch.
๐ŸŒ
javaspring
javaspring.net โ€บ blog โ€บ maven-how-to-run-a-java-file-from-command-line-passing-arguments
Maven: How to Run a Java File (Main.java) from Command Line with Arguments โ€“ Step-by-Step Guide Including Class Path Setup
Use mvn clean package to build the project. Run compiled classes with java -cp target/classes com.example.Main args. For executable JARs, configure maven-jar-plugin and use java -jar.
๐ŸŒ
Apache Maven
maven.apache.org โ€บ guides โ€บ getting-started
Maven Getting Started Guide โ€“ Maven
How does this work? Firstly, the parent POM created (called app), has a packaging of pom and a list of modules defined. This tells Maven to run all operations over the set of projects instead of just the current one (to override this behaviour, you can use the --non-recursive command line option).
๐ŸŒ
GitHub
github.com โ€บ sohamkamani โ€บ mvn-example
GitHub - sohamkamani/mvn-example: An example for how to run a java application on the command line with maven ยท GitHub
This is the example repo for my blog post on Running a Java Application on the Command Line With Maven
Starred by 2 users
Forked by 6 users
Languages ย  Java
๐ŸŒ
Mkyong
mkyong.com โ€บ home โ€บ maven โ€บ maven โ€“ how to create a java project
Maven - How to create a Java project - Mkyong.com
October 24, 2018 - D:\>mvn archetype:generate -DgroupId=com.mkyong.hashing -DartifactId=java-project -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false ... [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 3.992 s [INFO] Finished at: 2018-09-27T17:15:57+08:00 [INFO] ------------------------------------------------------------------------ Above command will generate a Java project from maven-archetype-quickstart template.
๐ŸŒ
TheServerSide
theserverside.com โ€บ tutorial โ€บ How-to-install-Maven-and-build-apps-with-the-mvn-command-line
How to install Maven and build apps with the mvn command line | TheServerSide
May 9, 2018 - Maven has stringent expectations on how to structure a software project, so it's best to ask the mvn command-line utility to create a Java project, rather than to create one by hand.
๐ŸŒ
TOOLSQA
toolsqa.com โ€บ maven โ€บ create-new-maven-project
How to Create a New Maven Project
How to Install Maven in Eclipse IDE, Step by step process to set up Java Maven Build tool in Eclipse with Selenium Webdriver. ... Maven Introduction, Step by step tutorial to set up Maven Build tool Integration with Sleenium Webdriver and jenkins Integartion ... How to Create a New Maven Project in Eclipse, Run your first Maven Test, Add Dependencies.
Top answer
1 of 1
4

There is Maven build plugin that would bundle all your dependencies in a JAR. You could execute it simply with

java -jar mybundled.jar

without caring about class path dependencies.

And here comes the Maven plugin. Replace the MainClass with your entry point.

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

As it is bound to the package lifecycle, you would find your bundled JAR in your target directory after doing a ...

mvn package

Regarding your concern that your code won't run on another machine, Java is platform-independent and generally you can rely on that. However, three things come to my mind that can make life hard:

  1. Your code depends on the system encoding. Maven should give you a warning on that in the log, advising you to specify it explicitly.
  2. You interact with the runtime, like executing system commands. ps won't work on windows and dir /s probably not on *nix.
  3. The version of your Java environment differ. Of course, JavaFX code won't run with a Java 1.5. If you use the latest features, make sure your clients already have the respective version or, at least, are able to get it from somewhere. You might want to take a look at the source and target compile options, too: http://docs.oracle.com/javase/7/docs/technotes/tools/windows/javac.html#crosscomp-example
๐ŸŒ
Medium
medium.com โ€บ @waqar.ah963 โ€บ how-to-create-and-build-a-maven-project-using-the-terminal-step-by-step-9e57b836e500
๐Ÿงฑ How to Create and Build a Maven Project Using the Terminal (Step-by-Step) | by Waqar Ahmed | Medium
May 26, 2025 - Apache Maven is a build automation tool used primarily for Java projects. It helps you: ... Letโ€™s get into how you can create your first Maven project using just the terminal. Before you begin, make sure you have the following installed: ... mvn archetype:generate \ -DgroupId=com.example.learnjava \ -DartifactId=MyFirstMavenApp \ -DarchetypeArtifactId=maven-archetype-quickstart \ -DinteractiveMode=false ... After running this command, Maven will create a project folder named MyFirstMavenApp with the following structure: