I have been trying to get Maven set up with JavaFX. Even though I was unexperienced with Maven and JavaFX, I didn't expect it to be so much of a challenge. My Java knowledge is pretty solid (including Swing), and didn't expect to have this much difficulty getting it set up.
I started with a JavaFX project supplied by IntelliJ 13.0 Community Edition. The code in my Main class is relatively small:
package sample;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Main extends Application
{
#Override
public void start(Stage primaryStage) throws Exception
{
//getClass().getResource("../../resources/sample.fxml");
Parent root = FXMLLoader.load(getClass().getResource("../sample.fxml"));
primaryStage.setTitle("Hello World");
primaryStage.setScene(new Scene(root, 300, 275));
primaryStage.show();
}
public static void main(String[] args)
{
launch(args);
}
}
and my pom.xml isn't too big either:
<?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>sample</groupId>
<artifactId>JavaFXDemo</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<organization>
<name>RubberDucky</name>
</organization>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>com.zenjava</groupId>
<artifactId>javafx-maven-plugin</artifactId>
<version>2.0</version>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>com.zenjava</groupId>
<artifactId>javafx-maven-plugin</artifactId>
<version>2.0</version>
<configuration>
<mainClass>sample.Main</mainClass>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>com.oracle</groupId>
<artifactId>javafx</artifactId>
<version>2.2</version>
<systemPath>${java.home}/lib/jfxrt.jar</systemPath>
<scope>system</scope>
</dependency>
</dependencies>
</project>
I'm using the JavaFX Maven Plugin to build the application. After running mvn clean jfx:jar everything seems fine, all builds succeed. But when I try to run the application I get the following error:
Exception in Application start method
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.javafx.main.Main.launchApp(Main.java:698)
at com.javafx.main.Main.main(Main.java:871)
Caused by: java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(Unknown So
urce)
at com.sun.javafx.application.LauncherImpl.access$000(Unknown Source)
at com.sun.javafx.application.LauncherImpl$1.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NullPointerException: Location is required.
at javafx.fxml.FXMLLoader.load(Unknown Source)
at javafx.fxml.FXMLLoader.load(Unknown Source)
at javafx.fxml.FXMLLoader.load(Unknown Source)
at javafx.fxml.FXMLLoader.load(Unknown Source)
at javafx.fxml.FXMLLoader.load(Unknown Source)
at sample.Main.start(Main.java:15)
at com.sun.javafx.application.LauncherImpl$5.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl$5.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl$4$1.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl$4$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl$4.run(Unknown Source)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(Unknown Source)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.access$100(Unknown Source)
at com.sun.glass.ui.win.WinApplication$3$1.run(Unknown Source)
... 1 more
After some rough debugging the trouble seems to be in the path I am loading my files from. After hardcoding the sample.fxml to a place on my hard drive, the application runs without any problems. Same goes for the current setup (seen above) when running the application from IntelliJ.
I feel like I have exhausted every resource (including StackOverflow, some very similar errors) but I just can't figure out what exactly is wrong.
Make sure that your sample.fxml is in the src/main/resources/ directory (or a subdirectory). Then you can access the file like this:
Parent root = FXMLLoader.load(getClass().getClassLoader().getResource("sample.fxml"));
Explanation:
During the compile phase all resources and classes get copied to target/classes/. So your fxml file resides in this directory and your class in a subdirectory regarding its package name. If you call getClass().getResource("sample.fxml"); the file will be searched relative to the class file which will be this directory: target/classes/sample/.
Calling .getResource() on the classloader sets the relative search path to target/classes/ and therefore your file gets found.
P.S. You could also write:
Parent root = FXMLLoader.load(getClass().getResource("/sample.fxml"));
As #Absurd-Mind already explained, maven will not copy any resource files (like fxml) which resides under the src directory.
If you want to keep the fxml files besides the java source files, you can use the maven resource plugin to copy them:
<build>
...
<resources>
<resource>
<filtering>false</filtering>
<directory>src/main/java</directory>
<includes>
<include>**/*.fxml</include>
</includes>
</resource>
</resources>
...
</build>
I had the same issue with Intelij and Gradle.
Steps to fix:
1.Move file
sample.fxml
to path
\src\main\resources\fxml
Set path on this file:
Parent root =
FXMLLoader.load(getClass().getResource("/fxml/sample.fxml"));
For those who use gradle as their build system add this to your build.gradle file:
sourceSets.main.resources {
srcDirs = ["src/main/java"]; //assume that your java classes are inside this path
exclude "**/*.java"
}
Then clean and rebuild your project.
So what will it do? If you have a view.fxml inside your com.example.myfxapp package, after building your project it will exported to <build dir>/resources/com/example/myfxapp/view.fxml
For me I had took care of following things -
Make sure fxml file is placed where your main class resides. And then load it using this - (In my case, TechJFX.fxml is the file)
FXMLLoader loader = new FXMLLoader(getClass().getResource("TechJFX.fxml"));
Add opencv-*.jar to your project folder (In my case opencv-300.jar)
Add the jar to the build path using
Right click on the project folder -> Build Path -> Configure Build Path -> Libraries Tab -> Add External JARS and locate the jar
Also, under Order and Export -> Tick opencv-300.jar and JRE System Library(if not already ticked)
Copy opencv_java*.dll (In my case opencv_java300.dll) to the same location where you jar is in the project folder.
Add the following method to your main class
static{
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
}
Done.
Hope this helps someone facing similar issue.
my case is installed the javafx sdk to C:\Program Files\Java\openjfx-11.0.2_windows-x64_bin-sdk.
And root cause is the the space contained in the sdk path name.
Fixed by install the javafx sdk to folder which name without space, such as C:\javafx-sdk-11.0.2\lib
I have similar exception in my Application.
This is how I solved it The application is using the WinPcap 4.1.3 library, I installed it on my computer Exception disappeared
if your application uses a library and it is not installed on your computer, you must install it on your computer.
Related
I have two maven projects targeting java 10:
Project A depends on project B:
I've created run configuration for project A, which works as expected. Now, I want to create runnable jar from this run configuration,
... but .jar file doesn't contain .class file from project B:
So when I try to run this .jar it throws:
Exception in thread "main" java.lang.NoClassDefFoundError: b/B
at a.A.main(A.java:7)
Caused by: java.lang.ClassNotFoundException: b.B
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(Unknown Source)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(Unknown Source)
at java.base/java.lang.ClassLoader.loadClass(Unknown Source)
... 1 more
How can I fix this?
I've tested it on freshly dowloaded Eclipse Photon (4.8.0)
Ok, I've found an answer. I have to add also dependent project (B) to maven dependencies:
<dependencies>
<dependency>
<groupId>B</groupId>
<artifactId>B</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
As I beleive it wasn't required in previous Eclipse versions.
I am new to Scala. I have this code:
import org.apache.hadoop.conf.Configuration
import org.apache.hadoop.fs.{Path, FileSystem}
/**
* Created by serban on 19/01/16.
*/
object TestHadoop {
def main(args: Array[String]):Unit = {
val namenodeEndpoint = "hdfs://123.45.123.45:8020"
val conf = new Configuration
conf.set("fs.defaultFS", namenodeEndpoint)
val fs = FileSystem.newInstance(conf)
val path = new Path("/user/ubuntu")
val fileStatus = fs.listFiles(path,false)
println("Hello world "+fileStatus.getClass)
while(fileStatus.hasNext())
{
println("FS: "+fileStatus.next())
}
}
}
When I run it from Maven, it runs OK. But I moved the compiled class to another machine and ran it in the command line via scala TestHadoop. This is what I get:
java.lang.NoClassDefFoundError: org/apache/hadoop/conf/Configuration
at TestHadoop$.main(TestHadoop.scala:13)
at TestHadoop.main(TestHadoop.scala)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at scala.tools.nsc.util.ScalaClassLoader$$anonfun$run$1.apply(ScalaClassLoader.scala:78)
at scala.tools.nsc.util.ScalaClassLoader$class.asContext(ScalaClassLoader.scala:24)
at scala.tools.nsc.util.ScalaClassLoader$URLClassLoader.asContext(ScalaClassLoader.scala:88)
at scala.tools.nsc.util.ScalaClassLoader$class.run(ScalaClassLoader.scala:78)
at scala.tools.nsc.util.ScalaClassLoader$URLClassLoader.run(ScalaClassLoader.scala:101)
at scala.tools.nsc.ObjectRunner$.run(ObjectRunner.scala:33)
at scala.tools.nsc.ObjectRunner$.runAndCatch(ObjectRunner.scala:40)
at scala.tools.nsc.MainGenericRunner.runTarget$1(MainGenericRunner.scala:56)
at scala.tools.nsc.MainGenericRunner.process(MainGenericRunner.scala:80)
at scala.tools.nsc.MainGenericRunner$.main(MainGenericRunner.scala:89)
at scala.tools.nsc.MainGenericRunner.main(MainGenericRunner.scala)
Caused by: java.lang.ClassNotFoundException: org.apache.hadoop.conf.Configuration
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)
at scala.tools.nsc.util.ScalaClassLoader$URLClassLoader.scala$tools$nsc$util$ScalaClassLoader$$super$findClass(ScalaClassLoader.scala:88)
at scala.tools.nsc.util.ScalaClassLoader$class.findClass(ScalaClassLoader.scala:44)
at scala.tools.nsc.util.ScalaClassLoader$URLClassLoader.findClass(ScalaClassLoader.scala:88)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at scala.tools.nsc.util.ScalaClassLoader$URLClassLoader.scala$tools$nsc$util$ScalaClassLoader$$super$loadClass(ScalaClassLoader.scala:88)
at scala.tools.nsc.util.ScalaClassLoader$class.loadClass(ScalaClassLoader.scala:50)
at scala.tools.nsc.util.ScalaClassLoader$URLClassLoader.loadClass(ScalaClassLoader.scala:88)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
It has a problem with my first import. When I run it in Maven, Maven knows how to solve this dependency.
Question: What can I do to run this example on my machine from the command line?
Thanks.
Regards,
Serban
You have a NoClassDefFoundError error because you are simply invoking your class without passing to Scala the required external libraries. Maven has already any defined dependencies as part of its classpath, hence external libraries are detected at runtime.
You need to use the -cp option of Scala and pass to it the required jar files (the external libraries), although this approach may be error prone and not maintainable for large set of dependencies.
Alternatively, you could add to your pom the following:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.2</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<finalName>${artifactId}-${version}-with-dependencies</finalName>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
Note: if you already have plugins configured, just add the plugin element of the Maven Shade Plugin.
This configuration will create a far jar using your artifactId and version and adding as a suffix the -with-dependencies token. Just run:
mvn package
And you will find the new jar in the target folder.
You can then use this jar (and not just the compiled file) in another machine and run it as following:
scala -cp yourproject-yourversion-with-dependencies.jar TestHadoop
I'm trying to execute this example program, but I am getting the following class not found exception:
javax.naming.NoInitialContextException: Cannot instantiate class: org.apache.qpid.amqp_1_0.jms.jndi.PropertiesFileInitialContextFactory [Root exception is java.lang.ClassNotFoundException: org.apache.qpid.amqp_1_0.jms.jndi.PropertiesFileInitialContextFactory]
at javax.naming.spi.NamingManager.getInitialContext(Unknown Source)
at javax.naming.InitialContext.getDefaultInitCtx(Unknown Source)
at javax.naming.InitialContext.init(Unknown Source)
at javax.naming.InitialContext.<init>(Unknown Source)
at filternet.SimpleSenderReceiver.<init>(SimpleSenderReceiver.java:30)
at filternet.SimpleSenderReceiver.main(SimpleSenderReceiver.java:60)
Caused by: java.lang.ClassNotFoundException: org.apache.qpid.amqp_1_0.jms.jndi.PropertiesFileInitialContextFactory*
I have included the following dependency in my pom file:
<dependency> <groupId>org.apache.qpid</groupId>
<artifactId>qpid-jms-client</artifactId> </dependency>
But I can't find PropertiesFileInitialContextFactory class file nor any jndi directories in the local maven repository.
Solved by downloading the jars separately and installing them into the Maven repository according to this document:
How to add local jar files in maven project?
Then adding the pom definitions from the jar installs into the project's pom.xml file.
I have a class that I coded in a standard java project in eclipse on OSX (Java 1.6). The class uses classes and interfaces from another library.
I select the class, then do Run As > Java Application and all works well.
After that I try to run my project as a Maven project and things start to get a little frustrating.. I summarise all the steps here hoping that someone will tell me what I am doing wrong:
- From the standard java project I right click and did Configure > Convert to Maven project and clicked Finnish. All good so far.
Then Build Path > Configure Build Path > and add the folder that contains my project. Still good
THEN I remove all the #Override annotations since I read somewhere on SO that Maven uses JDK 1.5 instead of 1.6. Whatever, I remove them and my red flags go away. At this point my class looks exactly like in the original java project (except for the #override that I removed)
THEN I do Maven > clean. I get build success
THEN Maven > Install. I get a build success
THEN I select my class and do Run As > Java Application and I get this ugly looking trace:
Exception in thread "main" java.lang.NoClassDefFoundError: LMAXTrading/algos/HeartbeatClient
Caused by: java.lang.ClassNotFoundException: LMAXTrading.algos.HeartbeatClient
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
I don't know where to go from here. You can imagine that I went through a lot of trials and errors and searched on SO and elsewhere to find a way to get this to work. But I just cannot figure out what is wrong. So if someone has an idea I am so ready to listen.
I am pasting below my directory layout from the Navigator View as well as from the Package explorer view
And here is the POM.xml where I have added the JRE config
<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>someproject</groupId>
<artifactId>someproject</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
The project directory structure does not match with the default Maven directory structure. Maven expects the source to be in src/main/java directory under the root folder (the folder containing pom.xml). The source here is in the src folder hence the No sources to compile error.
You can either move your sources to use the default Maven directory structure or change the pom.xml to explicitly specify your source directory by adding the following to the build section of the pom:
<sourceDirectory>${basedir}/src</sourceDirectory>
More info on Maven's standard directory layouts can be found here.
I have a very simple HelloWorld code in Java which works ok. I'm using Eclipse and trying to figure out how to import dependencies for a project with the maven2 eclipse plugin.
public class testMavenDep {
public static void main(String arg[]){
System.out.println("Hello World");
}
}
However, when I right click on the project > configure > convert to maven project, and then try and run it gives me an error message saying...
Could not find the main class: testMavenDep.testMavenDep. Program will exit.
And the following in the console...
java.lang.NoClassDefFoundError: testMavenDep/testMavenDep
Caused by: java.lang.ClassNotFoundException: testMavenDep.testMavenDep
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)
Exception in thread "main"
My pom file is...
<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>testMavenDep</groupId>
<artifactId>testMavenDep</artifactId>
<version>0.0.1-SNAPSHOT</version>
</project>
My question is, for an already existing Java Project, what is the proper way to add maven dependencies? I can add the dependencies using the above method but I'm getting issues with it losing track of the main class. Thanks in advance!
What is the source folder that you are putting your main class in? By default, Eclipse puts it in src, but maven conventions are src/main/java. It could be that adding maven dependencies is changing your source folders so that your class is not compiled.