How do I run a .java file from the command line, Maven? - java

I want to run my main class which is present in the src/comm instead of src/java/main.
I tried using:
mvn exec:java -Dexec.mainClass=src.communication.Launcher.Launcher
This statement gives me the "class not found exception".
The hierarchy is like src/communication is the source folder, then Launcher is package and the Launcher.java is the class file.
How can I execute this file from the command line as my main function is inside this class?

Use maven-jar-plugin and put in the manifest in your main class,
but it is better to use src/java/main than the other folder.
<build>
<plugins>
<plugin>
<!-- Build an executable JAR -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
<configuration>
<archive>
<manifest>
<mainClass>pacakgeAndClasseName</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>

Related

No main manifest attribute, but I have it in my jar

I am trying to run my java web app on AWS EC2 instance. The error I am getting is
no main manifest attribute, in app.jar
Going through other questions, I found that the problem can be with MANIFEST.mf file. I have that file under META-INF folder, however its missing Main Class tag.
I developed Java web app using servlets and stuff, but I didn't use SpringBoot, and I am unsure what the starting point of my program is.
Here is a part of my pom.xml:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.3.1</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.2.0</version>
</plugin>
I thought that maven-jar-plugin would solve this by itself, but unfortunately that is not the case.
As a side note, I tried both war and jar packaging.
Any ideas how to make this work? Should I hardcode something in MANIFEST.mf by myself?
My goal is to start my webapp on EC2 using this command:
java -jar app.jar
for the maven-jar-plugin there is a place to configure the manifest. I have it like this in my project
my project is years old so please have a look at the documentation for the plugin: https://maven.apache.org/plugins/maven-jar-plugin/examples/manifest-customization.html
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>webServer.main</mainClass>
</manifest>
</archive>
</configuration>
</plugin>

Jar executable but can not find the classpath for main class

We have this small program we made and we wanna create an executable jar. Screenshot of my maven dependency and the project structure The problem is that I use Maven install and create a jar and in my pom, I have this plugin:
<plugin>
<!-- Build an executable JAR -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>Main</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
I have been trying Project.Main and as in the code just Main ( Main) but I do always get this error in the terminal:
java -jar /home/haraldur/Desktop/Skolinn/508/Project/target/Project-1.0.jar
Error: Could not find or load main class Main
Caused by: java.lang.NoClassDefFoundError: javafx/application/Application
try dependency !?
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
</dependency>

How to load resources which are bundled in an executable jar file which was generated by maven?

I am trying to load my resources which I have bundled in a jar file.
Originally they where placed under src/main/resources/*.wav.
To generate my jar file I have used the maven-jar-plugin.
If I unpack the jar file I can see the resource files.
In my source code I am loading the files with
AudioSystem.getAudioInputStream(MyClass.class.getResourceAsStream("/my-file.wav"));
When I run the application via IntelliJ this seems to work.
However when I try to execute the generated jar file the resources cannot be loaded.
Did I miss something? Maybe the classpath?
EDIT
My pom.xml has the following build property:
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>MyMain</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<configuration>
<mainClass>MyMain</mainClass>
</configuration>
</plugin>
</plugins>
</build>
I figured out what the problem was.
The problem has not been with maven.
It was the source code, which should be changed to:
URL url = this.getClass().getClassLoader().getResource("my-resource.wav");
AudioSystem.getAudioInputStream(url)

Could not load main class in jar created by Netbeans using Maven

I created a Maven Java Application project. I have defined following tags in the pom file,
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>com.it.rng.dataservice.DataService</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
When I build the project in NetBeans, The jar is created in the target folder as expected. But when I try to run that jar using,
java -jar application.jar
i get the following error,
Error: Could not find or load main class com.it.rng.dataservice.DataService
I have checked the jar's contents and the class files are inside the directory as follows,
application.jar->com->it->rng->dataservice->DataService.class
I am not sure what is wrong here. Please let me know if I am missing anything.
Edit:
Also the application runs fine if I use the 'Run' command in the IDE.

Missing Main-Class attibute in MANIFEST.MF in Maven generated jar file

I am currently experimenting with Maven in Eclipse (m2e-Plugin) and tried to build and run the Hello World example project. However, when launching the generated jar, nothing happens. I checked the MANIFEST.MF and noticed that the Main-Class attribute was missing. After adding the attribute, the jar could be launched.
Why does Maven not add this attribute?
Have a look at this link:
https://maven.apache.org/shared/maven-archiver/examples/classpath.html#aAdd
You can find there how to configure your maven project to run specific class. You have to add maven-jar-plugin configuration with mainClass defined.
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
...
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>fully.qualified.MainClass</mainClass>
</manifest>
</archive>
</configuration>
...
</plugin>
</plugins>

Categories