Embedded java at desktop application - java

If it possible to build java desktop application with embedded JVM? I do not need to depend on the end user having the right JRE installed.
I build my application for Windows with l4j maven plugin.
Googling does not give needed results. Maybe you someone know how to do it with maven o gradle, not by some another utility like Avian, ProGuard and etc. (Embed a JRE in a Windows executable?)

The Maven plugin for Launch4j lets you generate the Launch4j
executable as part of the Maven build process. It supports Maven 2.0.4
and Launch4j 3.x.
See here
This is a sample of the configuration that you can try to use:
<plugin>
<groupId>com.akathist.maven.plugins.launch4j</groupId>
<artifactId>launch4j-maven-plugin</artifactId>
<executions>
...
<configuration>
...
<jre>
<!-- Specify path or minVersion or both. -->
<path>bundled JRE path (%VAR%)</path>
<bundledJre64Bit>true|false</bundledJre64Bit>
<bundledJreAsFallback>true|false</bundledJreAsFallback>
<minVersion>x.x.x[_xx]</minVersion>
<maxVersion>x.x.x[_xx]</maxVersion>
<jdkPreference>jreOnly|preferJre|preferJdk|jdkOnly</jdkPreference>
<runtimeBits>64|64/32|32/64|32</runtimeBits>
<!-- Heap sizes in MB and % of available memory. -->
<initialHeapSize>MB</initialHeapSize>
<initialHeapPercent>%</initialHeapPercent>
<maxHeapSize>MB</maxHeapSize>
<maxHeapPercent>%</maxHeapPercent>
<opt>text (%VAR%)</opt>
</jre>
...
</configuration>
</execution>
</executions>
</plugin>

Related

How to deploy Springboot app in Linux along with Maven dependencies

I have created a Spring Boot java application (REST Services) which uses Tomcat internally as web server on Windows machine using Eclipse as IDE. It uses JDK 1.8 & Maven as build system. Here I create jar file (Run as Maven Install ) and then invoke that jar file from command prompt in my windows machine. I test these REST services using POSTMAN on my Windows machine.
Now I have to get it working on an Linux machine which does not have UI. Can you please help me how to achieve same on Linux machine and how to get those dependencies on Linux machine.
first, make sure your Linux server have Java installed. Best match your local java version.
second, make use of maven plugin to generate a shell script which can kick off this project.
Below is an example
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>appassembler-maven-plugin</artifactId>
<version>1.10</version>
<!-- bind to package phase -->
<executions>
<execution>
<id>make-appassembly</id>
<phase>package</phase>
<goals>
<goal>assemble</goal>
</goals>
</execution>
</executions>
<configuration>
<!-- set alternative assemble directory -->
<assembleDirectory>${project.build.directory}/${project.artifactId}-${project.version}
</assembleDirectory>
<environmentSetupFileName>envSetup.sh</environmentSetupFileName>
<includeConfigurationDirectoryInClasspath>true</includeConfigurationDirectoryInClasspath>
<repositoryLayout>flat</repositoryLayout>
<repositoryName>lib</repositoryName>
<platforms>
<!-- <platform>windows</platform> -->
<platform>unix</platform>
</platforms>
<!-- Extra JVM arguments that will be included in the bin scripts -->
<extraJvmArguments>-Dlog4j.configuration=file:$BASEDIR/etc/log4j.properties
-Dapplication.properties=file:$BASEDIR/etc/XXX.properties
-Xms2048m
-Xmx12288m -server -showversion -XX:+UseConcMarkSweepGC
-DXXX.log.dir=XXX
-DXXX.app.id=XXX
</extraJvmArguments>
<programs>
<program>
<mainClass>com.xxx.App</mainClass>
<name>xxx.sh</name>
</program></programs>
</configuration>
</plugin>

Java compilation problem -- default methods not allowed [duplicate]

I imported a Maven project and it used Java 1.5 even though I have 1.6 configured as my Eclipse default Preferences->Java->Installed JREs.
When I changed the Maven project to use the 1.6 JRE it still had the build errors left over from when the project was using Java 1.5 (I described these build errors earlier in: I have build errors with m2eclipse but not with maven2 on the command line - is my m2eclipse misconfigured?)
I'm going to delete the project and try again but I want to make sure this time that it uses Java 1.6 from the start to see if this eliminates the build problems.
How do I make sure the project uses Java 1.6 when I import it?
The m2eclipse plugin doesn't use Eclipse defaults, the m2eclipse plugin derives the settings from the POM. So if you want a Maven project to be configured to use Java 1.6 settings when imported under Eclipse, configure the maven-compiler-plugin appropriately, as I already suggested:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.1</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
If your project is already imported, update the project configuration (right-click on the project then Maven V Update Project Configuration).
I added this to my pom.xml below the project description and it worked:
<properties>
<maven.compiler.source>1.6</maven.compiler.source>
<maven.compiler.target>1.6</maven.compiler.target>
</properties>
I wanted to add something to the answer already provided. maven-compiler-plugin by default will compile your project using Java 1.5 which is where m2e get's its information.
That's why you have to explicitly declare the maven-compiler-plugin in your project with something other then 1.5. Your effective pom.xml will implicitly use the default set in the maven-compiler-plugin pom.xml.
<project>
<!-- ... -->
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
Your JRE was probably defined in run configuration. Follow these steps in Eclipse to change the build JRE.
1) Right click on the project and select Run As > Run Configurations
2) From Run Configurations window, select your project build configuration on the left panel. On the right, you will see various tabs: Main, JRE, Refresh, Source,...
3) Click on JRE tab, you should see something like this
4) By default, Work Default JRE (The JRE you select as default under Preferences->Java->Installed JREs) will be used. If you want to use another installed JRE, tick the Alternate JRE checkbox and select your preferred JRE from the dropdown.
Here is the root cause of java 1.5:
Also note that at present the default source setting is 1.5 and the default target setting is 1.5, independently of the JDK you run Maven with. If you want to change these defaults, you should set source and target.
Reference : Apache Mavem Compiler Plugin
Following are the details:
Plain 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>com.pluralsight</groupId>
<artifactId>spring_sample</artifactId>
<version>1.0-SNAPSHOT</version>
</project>
Following plugin is taken from an expanded POM version(Effective POM),
This can be get by this command from the command line C:\mvn help:effective-pom I just put here a small snippet instead of an entire pom.
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<executions>
<execution>
<id>default-compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>default-testCompile</id>
<phase>test-compile</phase>
<goals>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>
Even here you don't see where is the java version defined, lets dig more...
Download the plugin, Apache Maven Compiler Plugin » 3.1 as its available in jar and open it in any file compression tool like 7-zip
Traverse the jar and findout
plugin.xml
file inside folder
maven-compiler-plugin-3.1.jar\META-INF\maven\
Now you will see the following section in the file,
<configuration>
<basedir implementation="java.io.File" default-value="${basedir}"/>
<buildDirectory implementation="java.io.File" default-value="${project.build.directory}"/>
<classpathElements implementation="java.util.List" default-value="${project.testClasspathElements}"/>
<compileSourceRoots implementation="java.util.List" default-value="${project.testCompileSourceRoots}"/>
<compilerId implementation="java.lang.String" default-value="javac">${maven.compiler.compilerId}</compilerId>
<compilerReuseStrategy implementation="java.lang.String" default-value="${reuseCreated}">${maven.compiler.compilerReuseStrategy}</compilerReuseStrategy>
<compilerVersion implementation="java.lang.String">${maven.compiler.compilerVersion}</compilerVersion>
<debug implementation="boolean" default-value="true">${maven.compiler.debug}</debug>
<debuglevel implementation="java.lang.String">${maven.compiler.debuglevel}</debuglevel>
<encoding implementation="java.lang.String" default-value="${project.build.sourceEncoding}">${encoding}</encoding>
<executable implementation="java.lang.String">${maven.compiler.executable}</executable>
<failOnError implementation="boolean" default-value="true">${maven.compiler.failOnError}</failOnError>
<forceJavacCompilerUse implementation="boolean" default-value="false">${maven.compiler.forceJavacCompilerUse}</forceJavacCompilerUse>
<fork implementation="boolean" default-value="false">${maven.compiler.fork}</fork>
<generatedTestSourcesDirectory implementation="java.io.File" default-value="${project.build.directory}/generated-test-sources/test-annotations"/>
<maxmem implementation="java.lang.String">${maven.compiler.maxmem}</maxmem>
<meminitial implementation="java.lang.String">${maven.compiler.meminitial}</meminitial>
<mojoExecution implementation="org.apache.maven.plugin.MojoExecution">${mojoExecution}</mojoExecution>
<optimize implementation="boolean" default-value="false">${maven.compiler.optimize}</optimize>
<outputDirectory implementation="java.io.File" default-value="${project.build.testOutputDirectory}"/>
<showDeprecation implementation="boolean" default-value="false">${maven.compiler.showDeprecation}</showDeprecation>
<showWarnings implementation="boolean" default-value="false">${maven.compiler.showWarnings}</showWarnings>
<skip implementation="boolean">${maven.test.skip}</skip>
<skipMultiThreadWarning implementation="boolean" default-value="false">${maven.compiler.skipMultiThreadWarning}</skipMultiThreadWarning>
<source implementation="java.lang.String" default-value="1.5">${maven.compiler.source}</source>
<staleMillis implementation="int" default-value="0">${lastModGranularityMs}</staleMillis>
<target implementation="java.lang.String" default-value="1.5">${maven.compiler.target}</target>
<testSource implementation="java.lang.String">${maven.compiler.testSource}</testSource>
<testTarget implementation="java.lang.String">${maven.compiler.testTarget}</testTarget>
<useIncrementalCompilation implementation="boolean" default-value="true">${maven.compiler.useIncrementalCompilation}</useIncrementalCompilation>
<verbose implementation="boolean" default-value="false">${maven.compiler.verbose}</verbose>
<mavenSession implementation="org.apache.maven.execution.MavenSession" default-value="${session}"/>
<session implementation="org.apache.maven.execution.MavenSession" default-value="${session}"/>
</configuration>
Look at the above code and find out the following 2 lines
<source implementation="java.lang.String" default-value="1.5">${maven.compiler.source}</source>
<target implementation="java.lang.String" default-value="1.5">${maven.compiler.target}</target>
Good luck.
Simplest solution in Springboot
I'll give you the simplest one if you use Springboot:
<properties>
<java.version>1.8</java.version>
</properties>
Then, right click on your Eclipse project: Maven > Update project > Update project configuration from pom.xml
That should do.
One more possible reason if you are using Tycho and Maven to build bundles, that you have wrong execution environment (Bundle-RequiredExecutionEnvironment) in the manifest file (manifest.mf) defined. For example:
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: Engine Plug-in
Bundle-SymbolicName: com.foo.bar
Bundle-Version: 4.6.5.qualifier
Bundle-Activator: com.foo.bar.Activator
Bundle-Vendor: Foobar Technologies Ltd.
Require-Bundle: org.eclipse.core.runtime,
org.jdom;bundle-version="1.0.0",
org.apache.commons.codec;bundle-version="1.3.0",
bcprov-ext;bundle-version="1.47.0"
Bundle-RequiredExecutionEnvironment: JavaSE-1.5
Export-Package: ...
...
Import-Package: ...
...
In my case everything else was ok. The compiler plugins (normal maven and tycho as well) were set correctly, still m2 generated old compliance level because of the manifest. I thought I share the experience.
Project specific settings
One more place where this can go wrong is in the project specific settings, in Eclipse.
project properties: click your project and one of the following:
Alt + Enter
Menu > Project > Properties
right click your project > project properties (last item in the menu)
click on "Java Compiler"
Uncheck "Enable project specific settings" (or change them all by hand).
Because of client requirements we had them enabled to keep our projects in 1.6. When it was needed to upgrade to 1.7, we had a hard time because we needed to change the java version all over the place:
project POM
Eclipse Workspace default
project specific settings
executing virtual machine (1.6 was used for everything)
In case anyone's wondering why Eclipse still puts a J2SE-1.5 library on the Java Build Path in a Maven project even if a Java version >= 9 is specified by the maven.compiler.release property (as of October 2020, that is Eclipse version 2020-09 including Maven version 3.6.3): Maven by default uses version 3.1 of the Maven compiler plugin, while the release property has been introduced only in version 3.6.
So don't forget to include a current version of the Maven compiler plugin in your pom.xml when using the release property:
<properties>
<maven.compiler.release>15</maven.compiler.release>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
</plugin>
</plugins>
</build>
Or alternatively but possibly less prominent, specify the Java version directly in the plugin configuration:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<release>15</release>
</configuration>
</plugin>
</plugins>
</build>
This picks up Line's comment on the accepted answer which, had I seen it earlier, would have saved me another hour of searching.
I found that my issue was someone committed the file .project and .classpath that had references to Java1.5 as the default JRE.
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.5">
<attributes>
<attribute name="owner.project.facets" value="java"/>
</attributes>
</classpathentry>
By closing the project, removing the files, and then re-importing as a Maven project, I was able to properly set the project to use workspace JRE or the relevant jdk without it reverting back to 1.5 . Thus, avoid checking into your SVN the .project and .classpath files
Hope this helps others.
If you want to make sure that newly created projects or imported projects in Eclipse use another default java version than Java 1.5, you can change the configuration in the maven-compiler-plugin.
Go to the folder .m2/repository/org/apache/maven/plugins/maven-compiler-plugin/3.1
Open maven-compiler-plugin-3.1.jar with a zip program.
Go to META-INF/maven and open the plugin.xml
In the following lines:
<source implementation="java.lang.String" default-value="1.5">${maven.compiler.source}</source>
<target implementation="java.lang.String" default-value="1.5">${maven.compiler.target}</target>
change the default-value to 1.6 or 1.8 or whatever you like.
Save the file and make sure it is written back to the zip file.
From now on all new Maven projects use the java version you specified.
Information is from the following blog post: https://sandocean.wordpress.com/2019/03/22/directly-generating-maven-projects-in-eclipse-with-java-version-newer-than-1-5/
To change JDK's version, you can do:
1- Project > Properties
2- Go to Java Build Path
3- In Libraries, select JRE System ... and click on Edit
4- Choose your appropriate version and validate

IntelliJ IDE 2017.3.2 Maven Project packagingexcludes

I have a maven project that is going out to tomcat using a exploded war build in development. I have the following entry in my pom to exclude certain files for any non development environments:
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
<configuration>
<warSourceDirectory>web</warSourceDirectory>
<failOnMissingWebXml>true</failOnMissingWebXml>
<webXml>web/WEB-INF/web.xml</webXml>
<packagingExcludes>
WEB-INF/classes/application.properties,
WEB-INF/classes/proxool.xml,
WEB-INF/classes/log4j.properties,
WEB-INF/classes/wetemplates/**
</packagingExcludes>
</configuration>
</plugin>
The packaging excludes are being used by intellij and excluded from my local development build and breaking it. I have been having to comment this block out in development for the environment to function correctly. How can I resolve this so that these are not excluded in my development environment?
You can use maven profiles to configure the build in such way that the Intellij build will use one given profile and the Maven/external build will use another profile.
Take a look at here: http://maven.apache.org/guides/introduction/introduction-to-profiles.html

How to configure MuleSoft with Maven?

I am trying to create my first MuleSoft application using Maven. I have used CMD to confirm that both Java and Maven are installed. I've also configured my environment variables.
I then open the MuleSoft Anypoint Studio, and create my first named "mulesoft-sample" I select to use Maven, and leave all default settings as they are.
Once I click finish, the Console begins to download a number of files
These continue to be downloaded until after it would seem the near end of the files, it throws an error:
There was an error running the studio:studio goal on project mulesoft-sample
In Console, I see the errors listed below.
I was originally trying to use the latest version of Maven (3.5.0). However, I found a video tutorial that said 3.3.9 was a verified version for MuleSoft. So I thought perhaps the newest version wasn't supported and instead used 3.3.9. However, it is still failing. I've also tried changing the environment variable to other options, such as going straight to \bin, rather than the complete directory (taking shots in the dark here). Unfortunately, nothing has worked and I'm running out of option.
Can anyone tell me why I'm getting these errors, and my build will not succeed? How to correct the error?
In anypoint studio go on windows>prefernces.then click on java>installed JREs. and add your Java JDK folder and note that only JDK, not JRE. then you can test your maven
This error occurs when the right java is not pointing with Anypoint Studio . In the studio navigate to windows > preferences the search java. Make sure jdk is selected and not jre . If still problem continues try updating your java and reinstalling maven .
Download the latest maven on your local machine, then goto anypoint
studio windows->preferences->anypoint studio->maven
provide the maven path and click Test Maven configuration(green
check mark obtained if the URL is properly provided)
Mule Maven Plugin configuration.
1.1. Use mule mule-app-maven-plugin to build the application.
<plugin>
<groupId>org.mule.tools.maven</groupId>
<artifactId>mule-app-maven-plugin</artifactId>
<version>${mule.tools.version}</version>
<extensions>true</extensions>
<configuration>
<copyToAppsDirectory>false</copyToAppsDirectory>
</configuration>
</plugin>
No need for additional Mule Maven plugins.
This is the default mule plugin set when creating your Mule project on AnyPoint (AP) Studio.
1.2. Make sure you set flag copyToAppsDirectory to false.
This flag is set to true by default so you must change it manually.
Maven Dependency Plugin Configuration.
2.1. Now we need to add a plugin to copy the generated artifact to the MULE_HOME/apps directory, i.e. to deploy the application to the standalone local mule server.
We will use the maven-dependency-plugin.
Just add these plugin after the mule maven plugin section in your application pom.xml.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.10</version>
<executions>
<execution>
<id>copy</id>
<phase>deploy</phase>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>com.mule.support.ba</groupId>
<artifactId>bonmarche-case-00145615</artifactId>
<version>1.0.0</version>
<type>zip</type>
<overWrite>false</overWrite>
<outputDirectory>C:/mule-home/apps</outputDirectory>
<destFileName>bonmarche-case.zip</destFileName>
</artifactItem>
</artifactItems>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>true</overWriteSnapshots>
</configuration>
</execution>
</executions>
</plugin>
2.2. Update the attributes within the tag (in grey) with the corresponding values on your environment.
2.3. As you see in 2.1, we are configuring the deployment to apps folder on Maven 'deploy' phase. You may choose a different phase to copy the artifact, but it must be bound to any phase after the package phase, so that the artifact exists in the repository.
Disable the default execution of Maven Deploy Plugin to prevent deploying the generated artifact to an external repository. This may be changed depending on your project needs.
Add this plugin after maven-dependency-plugin:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
Build your application and verify the generated ZIP file is copied to MULE_HOME/apps directory.
$> mvn clean deploy
I have faced same issue and got resolved by changing the JRE to JDK in preferences==>java==>Installed JRE's there change to JDK and restarted the any point studio. This resolved the issue to me.

Not able to start Tomcat for MAven Project inside Eclipse

Not able to start Tomcat for maven project in Eclipse I am getting:
java.lang.ClassNotFoundException: org.apache.struts2.dispatcher.FilterDispatcher
while starting tomcat.
the same is working outside eclipse(making the war and deploying in Tomcat)
Can any one suggest what to do?
My answer will be slightly off-topic but If I dare, I really recommend using jetty and using hot deployment in a separate process, not to launch it from within maven, but as a real server in /etc/init.d. Then you could deploy your app easily with an external script, activable from within eclipse, that will do a hot deploy of context in jetty.
It takes time to get this config but it's the fastest I have found and uses much less memory than using jetty within eclipse, and much much less than using tomcat. Hot deploy is also quite interesting in jetty.
You build cycle will look like
edit / save from eclipse
build / package through maven from eclipse (eclipse indigo is a charm for that)
deploy your app in jetty through a external tool script from eclipse.
Regards,
Stéphane
It has to do with tomcat looking for those classes in src/main/webapp/WEB-INF/lib, You can add the dependency plugin to your pom.xml like this.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.3</version>
<executions>
<execution>
<phase>install</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
</execution>
</executions>
<configuration>
<outputDirectory>src/main/webapp/WEB-INF/lib</outputDirectory>
</configuration>
</plugin>
This plugin will copy all the pom dependencies to WEB-INF/lib and tomcat can find all the jars when you it out of workspace

Categories