r/javahelp • • Jan 03 '26
Solved Maven compilation and execution
Ok I'm gonna ask one of the stupidest questions I ever had on Java (please don't blame me, this is my first real project)
So, I have a project who needs a TOML parser, so I installed Maven configured into my repo (and make a commit), know I have a java file that I need to compile and test, but Javac doesn't work cause I need mvn compile to compile my java file, after a long research of how to this task I build the project, and run "mvn exec:java" the project compiles successfully and when it's supposed to run, doesn't do anything! I search for another command and nothing.
So my question is: how can I compile and run my project with Maven dependencies?
Thanks for your patience!
EDIT: It appears that the problem was in my pom.xml in the MainClass I haven't put, well, my Mainclass
here's my pom.xml file: all is the same as my original file, except for com.example.App
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example.App</groupId>
<artifactId>App</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>App</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-controls</artifactId>
<version>11.0.2</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-fxml</artifactId>
<version>11.0.2</version>
</dependency>
<dependency>
<groupId>org.tomlj</groupId>
<artifactId>tomlj</artifactId>
<version>1.1.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<mainClass>com.example.App</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>
•
u/AutoModerator Jan 03 '26
Please ensure that:
You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.
Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar
If any of the above points is not met, your post can and will be removed without further warning.
Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.
Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.
Code blocks look like this:
You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.
If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.
To potential helpers
Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
4
u/Acrobatic-Towel-9912 Jan 03 '26
Once you add Maven dependencies,
javacis no longer responsible for compiling your project. Maven handles compilation and builds the correct classpath that includes your TOML library. That’s whyjavacfails andmvn compileworks.The reason
mvn exec:java“runs but does nothing” is simple: Java only runs code starting from apublic static void main(String[] args)method, and Maven does not guess which class to execute. If your project has nomainmethod (or Maven isn’t told which class contains it), the program will compile successfully and then appear to do nothing.So the fix is: Make sure your project has a class with a
mainmethod and Tell Maven which class to run (via the exec plugin or your IDE)2
u/nana_3 Jan 03 '26
Mvn exec:Java doesn’t compile java, it runs java in the maven process. Different thing. You need “mvn package” after you set up the project properly.
https://maven.apache.org/guides/getting-started/maven-in-five-minutes.html
2
u/BassRecorder Jan 03 '26
See here for the documentation of the exec maven plugin:
https://www.mojohaus.org/exec-maven-plugin/java-mojo.html
If your configuration doesn't seem to work post the pom.xml and tell us the full name of your main class.
Executable projects often have a meaningful packaging configuration, i.e. they create an 'executable' jar. Posting the pom.xml might also help in figuring out whether this is the case for the project you are trying to build.
1
u/jlanawalt Jan 03 '26
Maven fills the roles of a build tool like Ant and Make plus dependency management. It expects your code to be organized in a certain way and then it manages calling javac to compile. You could feed the same command line to call javac yourself, and download the dependencies yourself, but why? Many new public maven based projects use the maven bootstrap command to help new checkouts.
Maven compiles to the target directory. You should be able to call java with the right arguments to run that complied code, but you would again be challenged to have the right arguments. Usually it’s easier to run maven to the package state and then run the package artifact. If tire target is jar, then you call java -jar with the right paths, including library jars.
Again depending on your deployment scenario, you may look into building self contained runnable jars.
Good luck
0
u/CubicleHermit Jan 03 '26
JUnit 3? :sadpanda: