I'm following along with the tutorial at oracle and am getting this error message when I try to run the jar file output after the code is compiled.
Error: Could not find or load main class HelloWorld
To setup a java development environment, I went to oracle and downloaded their Java SE Development Kit and dropped it into /usr/lib/jvm/jdk1.8.0_45
and then pointed to it in the build.xml file as was indicated.
In following the tutorial, I have a project folder with the following code/script:
(example/src/HelloWorld.java)
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class HelloWorld extends Application {
@Override
public void start(Stage primaryStage) {
Button btn = new Button();
btn.setText("Say 'Hello World'");
btn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
System.out.println("Hello World!");
}
});
StackPane root = new StackPane();
root.getChildren().add(btn);
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
(example/build.xml)
<?xml version="1.0" encoding="UTF-8" ?>
<project name="JavaFX Hello World Example" default="default" basedir="."
xmlns:fx="javafx:com.sun.javafx.tools.ant">
<property name="JAVA_HOME" value="/usr/lib/jvm/jdk1.8.0_45"/>
<property name="build.src.dir" value="src"/>
<property name="build.classes.dir" value="classes"/>
<property name="build.dist.dir" value="dist"/>
<target name="default" depends="clean,compile">
<taskdef resource="com/sun/javafx/tools/ant/antlib.xml"
uri="javafx:com.sun.javafx.tools.ant"
classpath="${JAVA_HOME}/lib/ant-javafx.jar"/>
<fx:application id="HelloWorldID"
name="JavaFXHelloWorldApp"
mainClass="HelloWorld"/>
<fx:resources id="appRes">
<fx:fileset dir="${build.dist.dir}" includes="HelloWorld.jar"/>
</fx:resources>
<fx:jar destfile="${build.dist.dir}/HelloWorld.jar">
<fx:application refid="HelloWorldID"/>
<fx:resources refid="appRes"/>
<fileset dir="${build.classes.dir}"/>
</fx:jar>
<fx:deploy width="300" height="250"
outdir="." embedJNLP="true"
outfile="helloworld">
<fx:application refId="HelloWorldID"/>
<fx:resources refid="appRes"/>
<fx:info title="JavaFX Hello World Application"
vendor="Oracle Corporation"/>
</fx:deploy>
</target>
<target name="clean">
<mkdir dir="${build.classes.dir}"/>
<mkdir dir="${build.dist.dir}"/>
<delete>
<fileset dir="${build.classes.dir}" includes="**/*"/>
<fileset dir="${build.dist.dir}" includes="**/*"/>
</delete>
</target>
<target name="compile" depends="clean">
<javac includeantruntime="false"
srcdir="${build.src.dir}"
destdir="${build.classes.dir}"
fork="yes"
executable="${JAVA_HOME}/bin/javac"
source="1.8"
debug="on">
</javac>
</target>
</project>
Here's some very light debugging info which may expose the problem:
$ java -jar HelloWorld.jar
Error: Could not find or load main class HelloWorld
$ jar tvf HelloWorld.jar
0 Mon Apr 27 00:57:58 CDT 2015 META-INF/
113 Mon Apr 27 00:57:58 CDT 2015 META-INF/MANIFEST.MF
1014 Mon Apr 27 00:57:58 CDT 2015 HelloWorld$1.class
1436 Mon Apr 27 00:57:58 CDT 2015 HelloWorld.class
(HwlloWorld.jar META-INF/MANIFEST.MF)
Manifest-Version: 1.0
JavaFX-Version: 8.0
Class-Path:
Created-By: JavaFX Packager
Main-Class: HelloWorld
What's missing?
The message "Could not find or load main class HelloWorld" means that Java was not able to find the HelloWorld
class in its classpath. I suspect this may be due to the Class-Path
entry being empty in the manifest file:
Class-Path:
You may want to try overriding this entry to set the classpath to the root of the Jar by adding the following in the Ant buildfile (this is similar to the example in this link):
<fx:jar destfile="${build.dist.dir}/HelloWorld.jar">
<fx:application refid="HelloWorldID"/>
<fx:resources refid="appRes"/>
<fileset dir="${build.classes.dir}"/>
<manifest>
<attribute name="Class-Path" value="."/>
</manifest>
</fx:jar>
I have a working ant build for my Javafx application(it's 2.2, but it might work for you), the application is tested on win and linux.
These are the key differences:
<path id="fxant">
<filelist>
<file name="${java.home}\..\lib\ant-javafx.jar"/>
<file name="${java.home}\lib\jfxrt.jar"/>
</filelist>
</path>
<taskdef resource="com/sun/javafx/tools/ant/antlib.xml"
uri="javafx:com.sun.javafx.tools.ant"
classpathref="fxant"/>
Moreover when I check the manifest file, I see something like this:
JavaFX-Application-Class: packagename.MainClass
JavaFX-Class-Path: packagename/MainClass.class
These are automatically added and I don't have Main-Class and Class-Path.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With