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.
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 use Windows10 Pro 32bit ENG, EclipseEE Mars, Java 8_66. If I want create a easy bean project with Spring and run it it show me a error in Windows dialog. When I enter this dialog it generated exception.
Java Virtual Machine Launcher - Error :
A JNI error has occurred, please check your installation and try it again.
I have in CLASSPATH : Apache *.jars
•commons-logging-1.2.jar
•commons-logging-1.2-javadoc.jar
Spring *.jars
•spring-aop-4.1.5.RELEASE.jar
•spring-beans-4.1.5.RELEASE.jar
•spring-context-4.1.5.RELEASE.jar
•spring-aspects-4.1.5.RELEASE.jar
•spring-context-support-4.1.5.RELEASE.jar
•spring-core-4.1.5.RELEASE.jar
•spring-web-4.1.5.RELEASE.jar
•spring-webmvc-4.1.5.RELEASE.jar
•spring-expression-4.1.5.RELEASE.jar
I have the tested Java 8_60 vs Spring 4.1.5, Java 8_66 vs Spring 4.1.5 or 4.2.2.
KlientMetaTest.java
package klient;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import server.ServerVypis;
public class KlientMetaTest {
public static void main(String[] args) {
#SuppressWarnings("resource")
ApplicationContext context = new ClassPathXmlApplicationContext("META-INF/Beans.xml");
ServerVypis obj = (ServerVypis) context.getBean("mujSpring"); // id beanu
obj.getMessage();
}
}
ServerVypis.java
package server;
public class ServerVypis {
private String message;
public void setMessage(String message){
this.message = message; }
public void getMessage(){
System.out.println("Zde je tvuj Spring vypis : " + message); }
}
beans.xml which is in /META-INF/ folder
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="mujSpring" class="server.ServerVypis">
<property name="message" value="Vypis z \META-INF\Bean.xml"/>
</bean>
</beans>
Exception which was showing in console :
Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/context/ApplicationContext
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Unknown Source)
at java.lang.Class.privateGetMethodRecursive(Unknown Source)
at java.lang.Class.getMethod0(Unknown Source)
at java.lang.Class.getMethod(Unknown Source)
at sun.launcher.LauncherHelper.validateMainClass(Unknown Source)
at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)
Caused by: java.lang.ClassNotFoundException: org.springframework.context.ApplicationContext
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)
... 7 more
I have faced the same problem.
I made some changes in my pom.xml. Ff you are using the dependency of javax mail as well aws sdk then make sure that javax mail should be declare before the aws sdk's.
If you change the sequence then it will show the above error as it's loading jars in wrong sequence.As this worked in mine case, Hope this will work for you!
Problem solved. After I install Windows10, it broke my access to folders when I have save the external jars of frameworks.
I'm testing joss javaswift following this link. I also added JAR files from here to my project. But it provides an error. What am I doing wrong?
package test;
import org.javaswift.joss.client.factory.AccountConfig;
import org.javaswift.joss.client.factory.AccountFactory;
import org.javaswift.joss.model.Account;
public class Test {
public static void main(String[] args)
{
String username = "user";
String password = "pwd";
String authUrl = "http://...";
String tenantName = "test";
AccountConfig config = new AccountConfig();
config.setUsername(username);
config.setPassword(password);
config.setAuthUrl(authUrl);
config.setTenantName(tenantName);
Account account = new AccountFactory(config).createAccount();
}
}
Exception in thread "main" java.lang.NoClassDefFoundError:
org/apache/http/conn/scheme/SchemeSocketFactory at
org.javaswift.joss.client.factory.AccountFactory.createClientImpl(AccountFactory.java:38)
at
org.javaswift.joss.client.factory.AccountFactory.createAccount(AccountFactory.java:28)
at test.TestSWIFT3.main(TestSWIFT3.java:23)
Caused by: java.lang.ClassNotFoundException:
org.apache.http.conn.scheme.SchemeSocketFactory 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) ... 3 more
I think that you have a problem with the dependencies of the project.
Generally speaking, you need to create a project and in the project create a pom file for download the dependencies dynamically using maven.
The structure of the project should have a pom.xml file in the general configuration.
The pom file should contain at minimum the next information.
<?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</groupId>
<artifactId>testJoss</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.javaswift</groupId>
<artifactId>joss</artifactId>
<version>0.9.7</version>
</dependency>
</dependencies>
</project>
The first line is the definition required for the pom in the project.
I create an example project with the proper libraries.
https://bitbucket.org/stackoverflowquestion/testjoss/src/b6f930cd02d85ec71c27d5560ca2fe34097ce4d4?at=master
The example project works fine and the issue with the libraries is not present. The example have the debug activated, so, it is possible to see if everything goes well.
Entry in my porm.xml for slf4j
enter code here
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.7</version>
<scope>compile</scope>
</dependency>
score code:
package test.test1;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Hello world!
*
*/
public class App
{
public static void main( String[] args )
{
//System.out.println( "Hello World!" );
Logger logger = LoggerFactory.getLogger("App.class");
logger.info("Test");
}
}
Compiling is successful through Maven, but during Running Exception is thrown.
Exception in thread "main" java.lang.NoClassDefFoundError: org/slf4j/LoggerFacto
ry at test.test1.App.main(App.java:14)
Caused by: java.lang.ClassNotFoundException: org.slf4j.LoggerFactory
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)
... 1 more
Should set the classpath manually for slf4j jar file??
Maven picks up the dependencies you defined in the pom.xml from your local repository so everything compiles fine. However when you launch the application from a script or commandline you need to specify where the necessary classes are found, through the classpath.
For eclipse, there's the m2e & m2e-wtp plugins which integrate maven with your IDE so you don't need to add the jars manually one by one. Instead you'll have a sort of "virtual maven library" which contains references to all your dependencies, like in the image below:
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.