Creating Windows EXE from Java(FX) through Eclipse [duplicate] - java

This question already has answers here:
How can I convert my Java program to an .exe file?
(16 answers)
Closed 4 years ago.
I can't create Windows software through Eclipse. Using "Run" does run the app as it should, but I would like to start it without Eclipse, preferably as an independent Windows app, using a mere double-click.
Although this suggests several approaches, they all require additional knowledge that I do not posses.
I have tried using Launch4J but it also requires additional knowledge. I am astounded that it is so difficult to create a simple Windows-Java-app.
EDIT
I am using this pom.xml:
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>foo</groupId>
<artifactId>bar</artifactId>
<version>1.0.0</version>
<name>baz</name>
<description></description>
<dependencies>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>com.zenjava</groupId>
<artifactId>javafx-maven-plugin</artifactId>
<version>8.8.3</version>
<configuration>
<mainClass>application.Main</mainClass>
<updateExistingJar>true</updateExistingJar>
<skipCopyingDependencies>true</skipCopyingDependencies>
<useLibFolderContentForManifestClasspath>true</useLibFolderContentForManifestClasspath>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>build-jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
But Eclipse gives this error message:
Failed to execute goal com.zenjava:javafx-maven-plugin:8.8.3:build-jar
the goal being
clean compile package -e
Maven is installed, trying to run it with minimal appendancies.

Launch4j I know it can make an exe Wrapper over the Java, and it is cross platform.
Check here:
http://launch4j.sourceforge.net/

First of all, you can't create "Windows-Java-App". Java applications run on JVM (Java Virtual Machine) which means you wont be getting an exe file. What you need is a JAR file. For info on how to create a JAR file in Eclipse, see here:
https://stackoverflow.com/a/21110972/4440179
For JavaFX follow this guide: https://wiki.eclipse.org/Efxclipse/Tutorials/Tutorial1

I recommend making a Maven project to help you build an executable JavaFX Jar.
Firstly you'll need to make sure that Eclipse has the Maven plugin Help -> About Eclipse IDE and make sure that Eclipse.org - m2e is installed.
Then make a Maven project New -> Other.. -> Maven Project, fill in the basic stuff to create the project then put your source files under src/main/java of this new project.
There should be a pom.xml file in the root of this new project; This file helps manage your project dependencies and build process.
To make easily build an executeable add the below plugins to your pom.xml file to modify the build process.
Then you can just right click your pom.xml then Run As -> Maven Build ... then put clean compile package -e as your goals, refresh your MyProject/target directory and find your Jar under MyProject/target/javafx/app/jarname.jar
Also, if you want, you can look into using the launch4j maven plugin to have maven build the native executable for you, else your Jar will require the user have Java installed but it will still be a double-clickable JavaFX Jar.
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>foo</groupId>
<artifactId>bar</artifactId>
<version>1.0.0</version>
<name>baz</name>
<description></description>
<dependencies>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>com.zenjava</groupId>
<artifactId>javafx-maven-plugin</artifactId>
<version>8.8.3</version>
<configuration>
<mainClass>path.to.my.Main</mainClass>
<updateExistingJar>true</updateExistingJar>
<skipCopyingDependencies>true</skipCopyingDependencies>
<useLibFolderContentForManifestClasspath>true</useLibFolderContentForManifestClasspath>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>build-jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>

Related

Maven clean install not including sqlite dependency for executable jar file

After using
mvn clean install
and then
java -jar executable.jar
I get this error:
java.lang.ClassNotFoundException: org.sqlite.JDBC
This is my pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>****</groupId>
<artifactId>****</artifactId>
<version>0.7-SNAPSHOT</version>
<packaging>jar</packaging>
<name>****</name>
<description>****</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.xerial</groupId>
<artifactId>sqlite-jdbc</artifactId>
<version>3.23.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.junit/junit5-engine -->
<dependency>
<groupId>org.junit</groupId>
<artifactId>junit5-engine</artifactId>
<version>5.0.0-ALPHA</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.tinyjee.jgraphx/jgraphx -->
<dependency>
<groupId>org.tinyjee.jgraphx</groupId>
<artifactId>jgraphx</artifactId>
<version>3.4.1.3</version>
</dependency>
</dependencies>
<build>
<sourceDirectory>******</sourceDirectory>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<configuration>
<mainClass>*****</mainClass>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.1</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>******</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
Running the program inside intelliJ works without problems. I included it there from the project structure.
I replaced names and directories with ****. This is a school project and I don't want my prof accusing me of providing solutions to other groups in case they find this stackoverflow entry.
Probably you are getting this only when you are running your jar because the dependencies are not available/packaged inside of it.
Try generating a "fat jar" (also known as uber-jar), it will package all your dependencies inside the jar:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
<configuration>
<finalName>YOUR_JAR_FINAL_NAME</finalName>
</configuration>
</plugin>
</plugins>
</build>
Documentation related to the maven-shade-plugin can be found in here
Since you are using a runnable jar file, you can follow this section of the documentation related to Executable Jars
Some background.
Maven install never installs dependencies;
it only installs the project that is build via the POM.
The installation of dependencies are a task that you must also perform
if you don't use either a "fat jar" (which I can't recommend) or use
the new spring boot executable jar implementation.
The classic "fat jar" is an amazingly terrible
(but often the only option)
solution for a problem like this.
Consider using Spring-Boot;
they have developed a new,
sane,
version of an executable jar file that includes the dependencies within the executable jar and adds them to the classpath at startup.
This functionality is similar to the functionality of a "war" file when it is added to
a JEE container.
Caveat: I don't work for Pivotal,
I just like much of their work (the Spring Framework).
Edit:
Here is a link to the
Executable Jar Section in the Spring Boot Reference.
It contains some details.

Eclipse maven build path issue in case of custom sourceDirectory

I have converted an existing Java project to a Maven project and Maven builds everything perfectly when using the command line.
When I import the same project into Eclipse and compile (by right-clicking the project -> runs as Maven build, it still compiles without any issue.
However, I am not able to see the source folder. When I check the build path it gives warning - build path entry is missing.
I am not using standard src/main/java since I had a pre-existing folder structure for the project which could not be changed.
Here's my pom (notice the sourceDirectory tag):
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycom</groupId>
<artifactId>maven</artifactId>
<version>17.4</version>
<name>maven</name>
<properties>
<projecrt.rootDir>../../java</projecrt.rootDir>
</properties>
<build>
<finalName>re</finalName>
<sourceDirectory>${projecrt.rootDir}</sourceDirectory>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<manifestEntries>
<Build-Version>${buildversion}</Build-Version>
</manifestEntries>
</archive>
</configuration>
</plugin>
</plugins>
</build>
</project>
and here's my directory structure:
Src/java
----maven/pom.xml
----com/mycom/<...> // application code
You can use this build-helper-maven-plugin
And configure it in your pom file this way
<project>
...
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<id>add-source</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>some directory</source>
...
</sources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
and now when you do mvn eclipse:eclipse the custom source folders will get added to the classpath entries in the .project file.
So, you wont need to do any manual configuration, your configuration will simply get build at run-time.
You can do the same for your test sources as well, if you have your tests in a custom source folder.
Hope this helps.
Once your Java project is imported in the Eclipse workspace, you can specify the actual src folder:
Click Open Java perspective Window > Open Perspective > Other... > Java to change to the Java perspective.
In Project layout group, change selection to Create separate source and output folders and edit Configure default... to modify Source folder name from "src" to "sources".
In the settings of your project (project => property => java build path), you can also change/add src folders:

Automatically generate Java from .proto with maven/m2e in Eclipse IDE

For my team, I'd like to configure maven/eclipse build to automatically generate Java code from *.proto files (in a project that uses gRPC). Currently one needs to run mvn generate-source or mvn protobuf:compile (as in plugin usage page). Or what is the same add Run configuration to invoke maven goal compile.
Whenever Eclipse Maven project is refreshed (Alt+F5) or IDE is restarted, project is rebuilt but without what should appear in target/generated, thus turning project into red. So one need to generate and refresh project (F5). UPDATE Eclipse has needed source folders configured in .clathpath file.
As I know that should be m2e connector, but I could only find one https://github.com/masterzen/m2e-protoc-connector for the oldest Googles plugin com.google.protobuf.tools:maven-protoc-plugin, that is even not mentioned currently at https://github.com/grpc/grpc-java
We use exactly referenced/recommended
<groupId>org.xolstice.maven.plugins</groupId>
<artifactId>protobuf-maven-plugin</artifactId>
that is:
<build>
<extensions>
<extension>
<groupId>kr.motd.maven</groupId>
<artifactId>os-maven-plugin</artifactId>
<version>1.4.1.Final</version>
</extension>
</extensions>
<plugins>
<plugin>
<groupId>org.xolstice.maven.plugins</groupId>
<artifactId>protobuf-maven-plugin</artifactId>
<version>0.5.0</version>
<configuration>
<protocArtifact>com.google.protobuf:protoc:3.1.0:exe:${os.detected.classifier}</protocArtifact>
<pluginId>grpc-java</pluginId>
<pluginArtifact>io.grpc:protoc-gen-grpc-java:1.0.1:exe:${os.detected.classifier}</pluginArtifact>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>compile-custom</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
Related:
Linking to generated Java protobuf code in Eclipse
looked at this but that author was using other older, not supported now plugin : Eclipse build loop caused by protobuf generated code (related to Maven Project Builder)
P.P.S That plugin https://github.com/igor-petruk/protobuf-maven-plugin however has continuation as https://github.com/os72/protoc-jar-maven-plugin
Instead of using org.xolstice.maven.plugins:protobuf-maven-plugin my team has used com.github.os72:protoc-jar-maven-plugin to generate the message classes. I believe they are the same since under the hood they all seem to be using the tools from Google.
I am not using any m2e connectors for this plugin (Edit: protoc-jar-maven-plugin's m2e connector is bundled with it so no extra installation is needed, which is why it seemed like I wasn't using one, but technically I was, but this doesn't really matter). Unfortunately the changes in the .proto file are not "automatically" propagated to the generated .java files, you need to manually run Maven or trigger the project to be built in Eclipse (instructions below), but fortunately the target/generated-sources file is not vanishing or emptying or anything strange like what you describe.
If you want to rebuild the .java files from the .proto classes without using mvn clean compile from the command line you can clean the Eclipse project . Project → Clean... → select your project → Select build option (only shows if you have "Build Automatically" from the Project menu is unchecked).
I was able to do this in the latest Eclipse Neon (it will probably work in later ones too, but I don't know for certain).
Below is the POM I am using. I don't think it requires any special explanation, my solution is to simply use a different plugin than the one you are using. (If some explanation is needed I'll be happy to provide it though.)
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>io.github.jacksonbailey</groupId>
<artifactId>protobuf-m2e-sample</artifactId>
<version>0.1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
<version>3.1.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>com.github.os72</groupId>
<artifactId>protoc-jar-maven-plugin</artifactId>
<version>3.1.0.1</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<protocVersion>3.1.0</protocVersion>
<inputDirectories>
<include>src/main/resources</include>
</inputDirectories>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
for protobuf-maven-plugin
Thanks to sergei-ivanov answer in https://github.com/xolstice/protobuf-maven-plugin/issues/16, that gave link https://github.com/trustin/os-maven-plugin#issues-with-eclipse-m2e-or-other-ides :
One need to download os-maven-plugin-x.x.x.Final.jar (the version as in your pomx.ml) and put it into the <ECLIPSE_HOME>/plugins directory.
After that Eclipse will generate source on project clean, including after Maven -update project... (Alt+F5), but not after Project -> Build (or with default Build Automatically). Also on IDE start it will not compile.
Yes, that is illogical:
Project - Clean will generate and compile Java source
but
Project - Build will not.
P.S. Raised Bug 507412
Both eclipse and vscode can automatically compile proto when changed.
<plugin>
<groupId>kr.motd.maven</groupId>
<artifactId>os-maven-plugin</artifactId>
<version>1.6.2</version>
<executions>
<execution>
<phase>initialize</phase>
<goals>
<goal>detect</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.xolstice.maven.plugins</groupId>
<artifactId>protobuf-maven-plugin</artifactId>
<version>0.6.1</version>
<configuration>
<protocArtifact>com.google.protobuf:protoc:3.12.0:exe:${os.detected.classifier}</protocArtifact>
<pluginId>grpc-java</pluginId>
<pluginArtifact>io.grpc:protoc-gen-grpc-java:1.32.1:exe:${os.detected.classifier}</pluginArtifact>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>compile-custom</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>8</source>
<target>8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
See: https://github.com/trustin/os-maven-plugin#issues-with-eclipse-m2e-or-other-ides

Maven Error: Could not find or load main class

I'm using a Java Maven program and I don't know what to enter as the <mainClass>. I've tried all kinds of things based off of numerous stackoverflow questions, but they are not solving the error.
Each time it says:
Maven Error: Could not find or load main class ...
I have this written inside my pom.xml (minus the ???)
<build>
...
<plugins>
...
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.5.3</version>
<configuration>
<descriptors>
<descriptor>src/main/assembly/jar-with-dependencies.xml</descriptor>
</descriptors>
<archive>
<manifest>
<mainClass> ??? </mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
...
</plugins>
...
</build>
How do I fix these errors?
I got this error using Maven, and I discovered the solution.
Error: Could not find or load main class com.mycompany.testapifactory.Main
I'm using java JDK version 1.7 on Linux, my pom.xml file was the default generated by Netbeans and I was using these commands to compile, which do work fine with a normal hello-world java application:
mvn clean compile
java -jar target/TestAPIFactory-1.0-SNAPSHOT.jar com.mycompany.testapifactory.Main
What happened:
It turns out my problem was that my Main method was extending something Exotic like this:
public class Main extends SomeExoticLibraryClass{
public static void main(String[] args){
//...
}
}
It was this extending of the main class that caused the above error.
TLDR solution:
Make sure your main class isn't extending any 3rd party classes. Refactor those out and away into their own classes. That error message is awful, and requires process of elimination to find out what to do.
Unless you need the 'maven-assembly-plugin' for reasons other than setting the mainClass, you could use the 'maven-jar-plugin' plugin.
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<index>true</index>
<manifest>
<mainClass>your.package.yourprogram.YourMainClass</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
You can see the plugin in practise in the ATLauncher.
The 'mainClass' element should be set to the class that you have the entry point to your program in eg:
package your.package.yourprogram;
public class YourMainClass {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
For me the problem was nothing to do with Maven but to do with how I was running the .jar. I wrote some code and packaged it as a .jar with Maven. I ran it with
java target/gs-maven-0.1.0.jar
and got the error in the OP. Actually you need the -jar option:
java -jar target/gs-maven-0.1.0.jar
Please follow the below snippet.. it works..
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.xyz</groupId>
<artifactId>test</artifactId>
<packaging>jar</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>TestProject</name>
<description>Sample Project</description>
<dependencies>
<!-- mention your dependencies here -->
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.1.1</version>
<configuration>
<archive>
<manifest>
<mainClass>com.xyz.ABC.</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Please note, you have to provide the full classified class name (class name including package name without .java or .class) of main class inside <mainClass></mainClass> tag.
I got it too, for me the problem got resolved after deleting the m2 folder (C:\Users\username.m2) and updating the maven project.
I got it too, the key was to change the output folder from bin to target\classes. It seems that in Eclipse, when converting a project to Maven project, this step is not done automatically, but Maven project will not look for main class based on bin, but will on target\classes.
specify the main class location in pom under plugins
<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.example.hadoop.wordCount.WordCountApp</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
add this to your pom.xml file:
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>sample.HelloWorldApplication</mainClass>
</transformer>
</transformers>
</configuration>
and add the class name of your project (full path) along with the package name like "com.packageName.className" which consists of the main method having "run" method in it.
And instead of your "???" write ${mainClass} which will automatically get the className which you have mentioned above.
Then try command mvn clean install and mvn -jar "jar_file_name.jar" server "yaml_file_name.yml"
I hope it will work normally and server will start at the specified port.
The first thing i would suggest is to use the correct configuration for predefined descriptors.
<project>
[...]
<build>
[...]
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.5.3</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
[...]
</project>
To configure the main class you need to know the package and name of the class you would like to use which should be given into <mainClass>...</mainClass> parameter.
Furthermore i recommend to stop using Maven 2 and move to Maven 3 instead.
I got this error(classNotFoundException for main class), I actually changed pom version , so did maven install again and then error vanished.
TLDR : check if packaging element inside the pom.xml file is set to jar.
Like this - <packaging>jar</packaging>. If it set to pom your target folder will not be created even after you Clean and Build your project and Maven executable won't be able to find .class files (because they don't exist), after which you get Error: Could not find or load main class your.package.name.MainClass
After creating a Maven POM project in Netbeans 8.2, the content of the default pom.xml file are as follows -
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany</groupId>
<artifactId>myproject</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>
Here packaging element is set to pom. Hence the target directory is not created as we are not enabling maven to package our application as a jar file. Change it to jar then Clean and Build your project, you should see target directory created at root location. Now you should be able to run that java file with main method.
When no packaging is declared, Maven assumes the packaging as jar. Other core packaging values are pom, war, maven-plugin, ejb, ear, rar. These define the goals that execute on each corresponsding build life-cycle phase of that package. See more here
this worked for me....
I added the following line to properties in pom.xml
<properties>
<maven-jar-plugin.version>3.1.1</maven-jar-plugin.version>
</properties>
For me, I added
<packaging>jar</packaging>
and removed the default spring-boot-maven-plugin
<!-- remove this plugin
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin> -->
Add the -Xdiag option at the execution. This is extra "diagnostic". This will not solve the issue but add more detailed error messages and root causes that help identifying the issue.
In a rare occasion, an emoji in the access path caused this type of error. Double check your directory names, maybe a non standard character makes all the fun!
I am late to the party in my case it was the image I was using that was causing the trouble
Working config
<plugin>
<groupId>com.google.cloud.tools</groupId>
<artifactId>jib-maven-plugin</artifactId>
<version>${jib-maven-plugin.version}</version>
<configuration>
<from>
<image>openjdk:11-jre-slim</image>
</from>
<to>
<image>${docker.image.prefix}/${project.artifactId}</image>
</to>
<container>
<mainClass>com.example.configserverApplication</mainClass>
<ports>
<port>8888</port>
</ports>
</container>
</configuration>
</plugin>
Failing one
<plugin>
<groupId>com.google.cloud.tools</groupId>
<artifactId>jib-maven-plugin</artifactId>
<version>${jib-maven-plugin.version}</version>
<configuration>
<from>
<image>openjdk:alpine</image>
</from>
<to>
<image>${docker.image.prefix}/${project.artifactId}</image>
</to>
<container>
<mainClass>com.example.configserverApplication</mainClass>
<ports>
<port>8888</port>
</ports>
</container>
</configuration>
</plugin>
My understanding is that
"The OpenJDK port for Alpine is not in a supported release by OpenJDK, since it is not in the mainline codebase. It is only available as early access builds of OpenJDK Project Portola. See also this comment. So this image follows what is available from the OpenJDK project's maintainers."
https://hub.docker.com/_/openjdk
it is weird that it fails with a one-line error and changing the image fixed it for me.

maven build with war and jar pushing odd artifacts to internal repo

I have a maven project where I am building a war file, but I am also using the maven-jar-plugin to build a jar in the same project.
--DISCLAIMER--
I know this is not the 'correct' way to do this, but there are some other issues occurring when splitting this into a jar project and a separate war project with some 3rd party plugins.
I am seeing some strange behavior with this. Below is my project structure.
warproject
-src
--main
---webapp
----WEB-INF
-----web.xml
---java
----com.test.myclass
-----test.java
-pom.xml
When I build this project, i get the correct war and jar file in my target directory, however in my local .m2 repo something strange happens. The war file that is installed is named correctly war-jar-0.0.1-SNAPSHOT.war, however the contents of this file are the contents of my jar file. This also occurs if I do the inverse. i.e. if I setup my project to build a jar and use the maven-war-plugin to build the war, the archives in my target directory are correct, but my local repo has jar file with the contents of my war file. Below is the pom file I am using.
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.test</groupId>
<artifactId>war-jar</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<jarName>${project.artifactId}-${project.version}-client</jarName>
</configuration>
<executions>
<execution>
<id>make-a-jar</id>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/java</directory>
</resource>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
</build>
</project>
The console output for this is the following, it shows that the jar is being uploaded as the war.
Installing /home/me/work/src/war-jar/target/war-jar-0.0.1-SNAPSHOT.jar to /home/me/.m2/repository/com/test/war-jar/0.0.1-SNAPSHOT/war-jar-0.0.1-SNAPSHOT.war
--UPDATE
I got this working, but I had to change the phase of my 'make-a-jar' execution to install from package. This works fine and the correct artifacts are uploaded, but I am still confused as to why this makes a difference. Obviously the artifact is generated at a different lifecycle phase, and hence is not around at the time of the original install for the project, hence the wrong file is not uploaded. This seems like a 'hack' and I would like to understand why this is behaving this way.
I'm answering my own questions since I didn't get any information that helped me get to my solution. See my update on my original question for my solution.
This also works,
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<executions>
<execution>
<!--
When also running maven-jar-plugin correct maven-war-plugin's ID
from "default-war" to "default-jar"
-->
<id>default-jar</id>
<phase>package</phase>
<goals><goal>war</goal></goals>
<configuration>
...
</configuration>
</execution>
</executions>
</plugin>
Refer to http://maven.apache.org/guides/mini/guide-default-execution-ids.html
To figure out why your project behaves as is, analyze the Effective POM.
You need to specify the configurations for the maven-install-plugin to achieve this. Add the following plugin config under <build>.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-install-plugin</artifactId>
<executions>
<execution>
<phase>install</phase>
<goals>
<goal>install-file</goal>
</goals>
<configuration>
<packaging>jar</packaging>
<artifactId>${project.artifactId}</artifactId>
<groupId>${project.groupId}</groupId>
<version>${project.version}</version>
<file>
${project.build.directory}/${project.artifactId}-${project.version}.jar
</file>
</configuration>
</execution>
</executions>
</plugin>

Categories