I have build a JavaFX application which can be executed by its jar file. However if i try to run it using the native exe bundle i am receiving two popups:
com/npap/dicomrouter/Main - no main class
failed to launch JVM
and the application fails to start.
This is my Main class (most of its part):
package com.npap.dicomrouter;
public class Main extends Application {
...
public static void main(String[] args) throws IOException {
try {
JUnique.acquireLock(appId);
alreadyRunning = false;
log.info("application is going to start running!!!!");
} catch (AlreadyLockedException e) {
alreadyRunning = true;
log.info("application is already running!!!!");
}
if (!alreadyRunning) {
Application.launch(Main.class, (java.lang
.String[])null);
}
}
#Override
public void start(Stage primaryStage) throws IOException {
...
try {
AnchorPane page = (AnchorPane) FXMLLoader.load(Main.class.getResource("FXMLDocument.fxml"));
Scene scene = new Scene(page, initWidth, initHeight);
primaryStage.setScene(scene);
Platform.setImplicitExit(false);
createTrayIcon(primaryStage);
primaryStage.setTitle(appTitle);
primaryStage.show();
} catch (IOException ex) {
log.log(Level.INFO, null, ex);
}
}
My build.xml is configured this way:
<target name="-post-jfx-deploy">
<fx:deploy width="${javafx.run.width}" height="${javafx.run.height}"
nativeBundles="all"
outdir="${basedir}/${dist.dir}" outfile="${application.title}">
<fx:application name="${application.title}"
mainClass="${javafx.main.class}"/>
<fx:resources>
<fx:fileset dir="${basedir}/${dist.dir}"
includes="*.jar"/>
</fx:resources>
<fx:info title="${application.title}"
vendor="${application.vendor}"/>
</fx:deploy>
</target>
I am building the application (Java FX 2.1) using Netbeans, in the way described here:
https://netbeans.org/kb/docs/java/native_pkg.html
and i have succesfully build and run other javafx applications with native exe bundle. I am also attaching a screenshot showing how i declare the main project's class.
So i profoundly assume that this is a project specific issue. During the build in the console i get:
The jar lib\hibernate-commons-annotations-4.0.2.Final.jar has a main class org.hibernate.annotations.common.Version that does not match the declared main com.xxxx.yyyy.Main
The jar lib\javassist-3.15.0-GA.jar has a main class javassist.CtClass that does not match the declared main com.xxxx.yyyy.Main
The jar lib\h2-1.3.176.jar has a main class org.h2.tools.Console that does not match the declared main com.xxxx.yyyy.Main
Apart from these jars, i use several other classes which contain "main" methods (are runnable).
#SuppressWarnings("unchecked")
public static void main(String args[]) throws Exception {
...
Most of them are 3-rd party utility classes.
I am also adding the contents of package.cfg file just FYI:
app.mainjar=DicomRouterAffidea0_2_2.jar
app.version=1.0
app.id=com.npap.dicomrouter
app.preferences.id=com/npap/dicomrouter
app.mainclass=com/npap/dicomrouter/Main
app.classpath=lib/antlr-2.7.7.jar lib/c3p0-0.9.2.1.jar lib/clibwrapper_jiio.jar .....
... //jars listing
Finally i am attaching 3 screenshots of Libraries, Compiling and Deployment Netbeans configuration. Hope that can also help:
Just answering my question since after some trials i managed to build a functional .exe file by removing the code from build.xml.
So i removed all these lines from build.xml:
<target name="-post-jfx-deploy">
<fx:deploy width="${javafx.run.width}" height="${javafx.run.height}"
nativeBundles="all"
outdir="${basedir}/${dist.dir}" outfile="${application.title}">
<fx:application name="${application.title}"
mainClass="${javafx.main.class}"/>
<fx:resources>
<fx:fileset dir="${basedir}/${dist.dir}"
includes="*.jar"/>
</fx:resources>
<fx:info title="${application.title}"
vendor="${application.vendor}"/>
</fx:deploy>
and i am just importing:
<import file="nbproject/build-impl.xml"/>
Related
I'm working on a Java project that exec Ant target task on runtime.
On my pom file I have this dependency:
pom.xml
<dependency>
<groupId>org.apache.ant</groupId>
<artifactId>ant</artifactId>
<version>${ant.version}</version>
</dependency>
I have to run <scp> task that is included in a target container task (called scp-test-target).
Below you can see the ant file:
deploy.xml
<?xml version="1.0" encoding="UTF-8"?>
<project name="test" basedir="./">
<target name="scp-test-target" description="scp desc">
<echo message="Test SCP"/>
<scp file="..." todir="..."/>
</target>
</project>
I have created a junit file to exec 'scp-test-target':
Test.java
#Test
public void testAnt1(){
Project antProject = new Project();
ProjectHelper projectHelper = ProjectHelper.getProjectHelper();
antProject.addReference("ant.projectHelper", projectHelper);
antProject.setUserProperty("ant.file", "deploy.xml");
antProject.fireBuildStarted();
antProject.init();
projectHelper.parse(antProject, new File("deploy.xml"));
antProject.executeTarget("scp-test-target");
antProject.fireBuildFinished(null);
}
But when I run my junit test, the program failed because it not find jsch library.
Problem: failed to create task or type scp
Cause: the class org.apache.tools.ant.taskdefs.optional.ssh.Scp was not found.
This looks like one of Ant's optional components.
So, how can I tell to Ant to link jsch libraries on my file system in programmatically way (so in my Test.java)?
Thanks in advance
I'm getting an error when I try to wrap the Jar to exe through Launch4j.
I have added the library already in the NetBeans IDE and also for the Project but still the error persists.
The Jar is running without any problems but when I try to wrap it, I'm getting always that issue warning:
Exception in thread "AWT-EventQueue-0" java.lang.NoClassDefFoundError: org/netbeans/lib/awtextra/AbsoluteLayout
....
....
Caused by: java.lang.ClassNotFoundException: org.netbeans.lib.awtextra.AbsoluteLayout
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 17 more
How can I resolve this?
<?xml version="1.0" encoding="UTF-8"?>
<!-- You may freely edit this file. See commented blocks below for -->
<!-- some examples of how to customize the build. -->
<!-- (If you delete it and reopen the project it will be recreated.) -->
<!-- By default, only the Clean and Build commands use this build script. -->
<!-- Commands such as Run, Debug, and Test only use this build script if -->
<!-- the Compile on Save feature is turned off for the project. -->
<!-- You can turn off the Compile on Save (or Deploy on Save) setting -->
<!-- in the project's Project Properties dialog box.-->
<project name="HelaTergo" default="default" basedir=".">
<description>Builds, tests, and runs the project HelaTergo.</description>
<import file="nbproject/build-impl.xml"/>
<!--
There exist several targets which are by default empty and which can be
used for execution of your tasks. These targets are usually executed
before and after some main targets. They are:
-pre-init: called before initialization of project properties
-post-init: called after initialization of project properties
-pre-compile: called before javac compilation
-post-compile: called after javac compilation
-pre-compile-single: called before javac compilation of single file
-post-compile-single: called after javac compilation of single file
-pre-compile-test: called before javac compilation of JUnit tests
-post-compile-test: called after javac compilation of JUnit tests
-pre-compile-test-single: called before javac compilation of single JUnit test
-post-compile-test-single: called after javac compilation of single JUunit test
-pre-jar: called before JAR building
-post-jar: called after JAR building
-post-clean: called after cleaning build products
(Targets beginning with '-' are not intended to be called on their own.)
Example of inserting an obfuscator after compilation could look like this:
<target name="-post-compile">
<obfuscate>
<fileset dir="${build.classes.dir}"/>
</obfuscate>
</target>
For list of available properties check the imported
nbproject/build-impl.xml file.
Another way to customize the build is by overriding existing main targets.
The targets of interest are:
-init-macrodef-javac: defines macro for javac compilation
-init-macrodef-junit: defines macro for junit execution
-init-macrodef-debug: defines macro for class debugging
-init-macrodef-java: defines macro for class execution
-do-jar: JAR building
run: execution of project
-javadoc-build: Javadoc generation
test-report: JUnit report generation
An example of overriding the target for project execution could look like this:
<target name="run" depends="HelaTergo-impl.jar">
<exec dir="bin" executable="launcher.exe">
<arg file="${dist.jar}"/>
</exec>
</target>
Notice that the overridden target depends on the jar target and not only on
the compile target as the regular run target does. Again, for a list of available
properties which you can use, check the target you are overriding in the
nbproject/build-impl.xml file.
-->
</project>
CSS:
Manifest-Version: 1.0
X-COMMENT: Main-Class will be added automatically by build
Class-Path: lib/AbsoluteLayout.jar
HTML:
<?xml version="1.0" encoding="UTF-8"?>
<launch4jConfig>
<dontWrapJar>false</dontWrapJar>
<headerType>gui</headerType>
<jar>C:\Users\ckoca\Documents\NetBeansProjects\HelaTergo\dist\HelaTergo.jar</jar>
<outfile>C:\Users\ckoca\OneDrive - COMPUTACENTER\Desktop\SerkoBib.exe</outfile>
<errTitle>Bitte Java vorerst installieren</errTitle>
<cmdLine></cmdLine>
<chdir>.</chdir>
<priority>normal</priority>
<downloadUrl>http://java.com/download</downloadUrl>
<supportUrl></supportUrl>
<stayAlive>false</stayAlive>
<restartOnCrash>false</restartOnCrash>
<manifest></manifest>
<icon>C:\Users\ckoca\Documents\Icons\Icons8-Windows-8-Business-Department.ico</icon>
<classPath>
<mainClass>helaTergo.HomeHelaTergo</mainClass>
<cp>lib/AbsoluteLayout.jar</cp>
</classPath>
<jre>
<path></path>
<bundledJre64Bit>false</bundledJre64Bit>
<bundledJreAsFallback>false</bundledJreAsFallback>
<minVersion>1.6.0</minVersion>
<maxVersion></maxVersion>
<jdkPreference>preferJre</jdkPreference>
<runtimeBits>64/32</runtimeBits>
</jre>
<versionInfo>
<fileVersion>1.0.0.0</fileVersion>
<txtFileVersion>1.0.0.0</txtFileVersion>
<fileDescription>Serko Library Software</fileDescription>
<copyright>Computacenter</copyright>
<productVersion>1.0.0.0</productVersion>
<txtProductVersion>1.0.0.0</txtProductVersion>
<productName>SerkoBib</productName>
<companyName>Computacenter</companyName>
<internalName>SerkoBib</internalName>
<originalFilename>SerkoBib.exe</originalFilename>
<trademarks></trademarks>
<language>ENGLISH_US</language>
</versionInfo>
</launch4jConfig>
This question already has an answer here:
How to include a .jar dependency into the ANT target that generate my final .jar file of my application?
(1 answer)
Closed 8 years ago.
I am pretty new in Ant and I have the following problem trying to create a build.xml file to compile a single class (that contains the main() method) command line application.
So this is the code of the Main class (at this time it is the only class in the application):
import java.sql.*;
import java.util.TimeZone;
public class Main {
public static void main(String[] args) {
System.out.println("Hello World !!!");
System.out.println(args.length);
if(args.length != 0) {
String partitaIVA = args[0];
String nomePDF = args[1];
}
Connection conn = null;
Statement stmt = null;
try {
Class.forName ("oracle.jdbc.OracleDriver");
TimeZone timeZone = TimeZone.getTimeZone("Etc/GMT+2");
TimeZone.setDefault(timeZone);
// Step 1: Allocate a database "Connection" object
conn = DriverManager.getConnection("jdbc:oracle:thin:#XXX.XXX.XXX.XXX:1521:eme1", "myUserName", "myPswd"); // Oracle DB driver
System.out.println("After obtained connection with DB");
} catch(SQLException ex) {
ex.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
}
}
As you can see the behavior of the main() method is very simple, at this time only obtain a connection with an Oracle database (represented by the Connection conn object). Running it into the IDE (I am using IntelliJ) it works fine (I can see it using the debugger, the **Connection conn is correctly set).
Now I am working on the following build.xml file for the Ant compilation:
<project name="edi-sta">
<description>
EDI-STA
</description>
<target name="clean">
<delete dir="build"/>
</target>
<target name="compile">
<mkdir dir="build/classes"/>
<javac srcdir="src" destdir="build/classes"/>
</target>
<target name="jar">
<mkdir dir="build/jar"/>
<jar destfile="build/jar/Main.jar" basedir="build/classes">
<manifest>
<attribute name="Main-Class" value="Main"/>
</manifest>
</jar>
</target>
<target name="run">
<java jar="build/jar/Main.jar" fork="true"/>
</target>
</project>
After that I have performed in order the clean, compile and jar targets I tried to open the console, access to the build/jar/ directory that contains the Main.jar file and I try to execute it performing the following statement:
C:\Projects\edi-sta\build\jar>java -jar Main.jar
Hello World !!!
0
java.lang.ClassNotFoundException: oracle.jdbc.OracleDriver
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at Main.main(Unknown Source)
C:\Projects\edi-sta\build\jar>
But, as you can see, now happens a very strange thing, it seems that can't found the class that contains the Oracle driver (oracle.jdbc.OracleDriver) so the ClassNotFoundException is thrown.
I think that this happens because if I open (with WinZip) my generated Main.jar file it only contains the Main.class file and the META-INF folder (that contains only the MANIFEST.MF file) but I have not the ojdbc6.jar file that contains the Oracle driver that I use.
So my question is: what have I to do to include this ojdbc6.jar dependency properly in my generated Main.jar file and avoid the ClassNotFoundException?
Tnx
You can define jar dependencies in the Class-Path attribute of the manifest file of the jar. Read the documentation here - http://docs.oracle.com/javase/tutorial/deployment/jar/downman.html
To achieve that from your ant task, use the Class-Path attribute as below
<jar destfile="build/jar/Main.jar" basedir="build/classes">
<manifest>
<attribute name="Main-Class" value="Main"/>
<attribute name="Class-Path" value="your-jar-file"/>
....
I have created a executable jar file using build.
<target name="deploy" depends="compile">
<jar jarfile="${deploy.home}/${app.name}.jar" basedir="${build.home}">
<manifest>
<attribute name="Main-Class" value="abc.xyz"/>
</manifest>
</jar>
Here the xyz call has the main method to execute the whole program..
public static void main(String[] args) {
File buildFile = new File("build.xml");
Project p = new Project();
p.setUserProperty("ant.file", buildFile.getAbsolutePath());
p.init();
ProjectHelper helper = ProjectHelper.getProjectHelper();
p.addReference("ant.projectHelper", helper);
helper.parse(p, buildFile);
p.executeTarget(p.getDefaultTarget());
}
The jar file is being created successfuly. When I execute the like java -jar appname.jar, it is throwing exception --
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/tools/ant/Project
at abc.xyz.main(xyz.java:20)
Caused by: java.lang.ClassNotFoundException: org.apache.tools.ant.Project
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
The project class not found. Its there in ant.jar.
But when i execute the abc.java class simply by on command prompt java abc its working fine.
Pleased help me to get the cause? and its solution.
Add required libraries to the class path of the jar
<target name="deploy" depends="compile">
<jar jarfile="${deploy.home}/${app.name}.jar" basedir="${build.home}">
<manifest>
<attribute name="Main-Class" value="abc.xyz"/>
<attribute name="Class-Path" value="ant.jar"/>
</manifest>
</jar>
If you need more than one jar you have to separate them by spaces.
I have tried invoking Ant programmatically, but I am encountering this error
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/tools/ant/launch/AntMain
I tried to run build.xml separately using the console and via right-clicking it in eclipse running as ant build. It runs with no problems.
My sample ant testing class
public class AntTest {
public static void main(String[] args) {
File buildFile = new File("build.xml");
Project p = new Project();
p.setUserProperty("ant.file", buildFile.getAbsolutePath());
p.init();
ProjectHelper helper = ProjectHelper.getProjectHelper();
p.addReference("ant.projectHelper", helper);
helper.parse(p, buildFile);
p.executeTarget(p.getDefaultTarget());
}
}
My sample build.xml
<?xml version="1.0" encoding="UTF-8"?>
<project name="testproject" default="test" basedir=".">
<target name="test">
<echo message="Hello World" />
</target>
</project>
What am I missing?
Make sure that the ant libraries are in your classpath.
It seems, that ant-launcher-VERSION.jar (where Version is the ant version you are using) is missing in your classpath.