Throwing Error Running slf4j-api through Maven - java

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:

Related

java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory even my class did have the jars

This took me some time to debug , but still no clue.
I am using m2e under Eclipse, I checked the java.class.path, it did have below jars on the classpath.
Any idea ?
Exception in thread "main" java.lang.NoClassDefFoundError:
org/slf4j/LoggerFactory
at hadoop.ReadHDFS.main(ReadHDFS.java:18)
Caused by: java.lang.ClassNotFoundException: org.slf4j.LoggerFactory
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
C:\Users\xxxx\.m2\repository\org\slf4j\slf4j-api\1.7.10\slf4j-api-1.7.10.jar
C:\Users\xxxx\.m2\repository\log4j\log4j\1.2.17\log4j-1.2.17.jar
C:\Users\xxxx\.m2\repository\org\slf4j\slf4j-log4j12\1.7.10\slf4j-log4j12-1.7.10.jar
---add more context---
I am running the hadoop program. just found that when i add the hadoop-client dependency in the pom.xml, it will abort this error .
In the pom.xml , i did not explicitly add these dependencies but the effective pom have them and the java.class.path also include them.
Compile is good and only error in runtime
---workaround---
It will work if i excluded the slf4j-api from hadoop-client :
As for the root cause, still unknown.

"Getting started" issue with JOSS JavaSWIFT

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.

Using maven ant plugin in javafx application, How to add Dependencies in javaFX jar. Still facing class not found exception

I using this guide added my dependencies to main jar JavaFX jar with dependencies bundled
i am facing dependencies issue which causing Class not found exception. My Pom is exactly like mentioned in above link. i use dependencies plugin to copy dependencies to lib folder in target.
Here is my stack trace
Try calling Class.forName(com.client.main.App) using classLoader = j
ava.net.URLClassLoader#3c9076d
found class: class com.client.main.App
launchApp: Try calling com.sun.javafx.application.LauncherImpl.launchApplication
Autoconfig of proxy is completed.
JavaFX: using com.sun.javafx.tk.quantum.QuantumToolkit
Exception in Application init 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 init 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.NoClassDefFoundError: com/google/inject/Module
at com.nuaxis.rpas.client.main.App.init(App.java:25)
... 4 more
Caused by: java.lang.ClassNotFoundException: com.google.inject.Module
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 java.lang.ClassLoader.loadClass(Unknown Source)
... 5 more
Done with invoke and wait
Maven Ant Task in maven pom file using maven ant plugin
<jfxjar destfile="${project.build.directory}/${project.build.finalName}">
<fileset dir="${project.build.directory}/classes" />
<fileset dir="${project.build.directory}/lib/" includes="*.jar" />
<application name="${project.name}" mainClass="com.client.main.App" />
<resources>
<fileset dir="${project.build.directory}/lib/" includes="*.jar" />
</resources>
</jfxjar>
Here is my Fx Application Class code
public class App extends Application {
/*public static Logger mLogger = LoggerFactory.getLogger(App.class); */
public static Injector injector;
public static AppHandler mAppHandler;
#Override
public void init() throws Exception {
// TODO Auto-generated method stub
super.init();
injector = Guice.createInjector(new GuiceModule());/*This line cause exception*/
mAppHandler = injector.getInstance(AppHandler.class);
if(mAppHandler!=null)
mAppHandler.startHandler();
}
#Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("/fxml/FXMLDocument.fxml"));
Scene scene = new Scene(root);
scene.getStylesheets().add("/styles/style.css");
stage.setTitle("RPAS Java Client");
stage.getIcons().add(new Image("/images/RPAS Java Client.png"));
stage.setScene(scene);
stage.show();
}
#Override
public void stop() throws Exception {
mAppHandler.stopHandler();
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
Here is my understanding
Now what i understand is my dependencies are not properly loading. ant "jar making" task makes jar without any issues. when i double click it through exception. I opened the jar and here is my Manifest
Manifest-Version: 1.0
JavaFX-Version: 2.2
JavaFX-Application-Class: com.client.main.App
JavaFX-Class-Path: guice-3.0.jar hamcrest-core-1.3.jar javax.inject-1.jar log4j-1.2.15.jar slf4j-api-1.7.7.jar slf4j-log4j12-1.7.7.jar
Created-By: JavaFX Packager
Main-Class: com/javafx/main/Main
I also wonder when i saw that jar making process add com.javafx.main.Main.java file to the code automatically which first check Run time(JRE and FX) and then if all successful invoke the com.sun.javafx.Application.LauncherImp .
can any body help me?
I had resolved the issue. see my final pom at this link where should i put installer resources (wxs file,dmg-script,icon) and how to configure maven antrun when deploying self contained app

Bizarre log4j error

Trying to use log4j's built-in JMSAppender to log messages to a local message queue (ActiveMQ).
I set up a test Driver that has an entry main method and that just instantiates the actual class I want to make the connection:
Driver.java:
public static void main(String[] args) {
Log4jAmqBridge bridge = new Log4jAmqBridge();
bridge.run();
}
Log4jAmqBridge.java:
public void run() {
// ...
}
If I leave my run method totally empty (no instructions) and run the Driver, I just get an empty console (Eclipse). But the second I add the following constructor to run():
JMSAppender appender = new JMSAppender();
I get the following error:
Exception in thread "main" java.lang.NoClassDefFoundError: javax/jms/JMSException
at test.jmsappender.Log4jAmqBridge.run(Log4jAmqBridge.java:11)
at test.Driver.main(Driver.java:11)
Caused by: java.lang.ClassNotFoundException: javax.jms.JMSException
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)
... 2 more
I checked the javadoc and it doesn't look like the JMSAppender no-arg constructor throws an exception.
Does anybody know whats going on?
Edit:
I am actually using Maven to manage my dependencies and have included the following dependency declarations in my pom:
<dependency>
<scope>compile</scope>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.6.4</version>
</dependency>
<dependency>
<scope>compile</scope>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.6.4</version>
</dependency>
Which puts the following jars under the Maven Dependencies in my project (which are also added to the Eclipse workbench classpath):
slf4j-api-1.6.4
slf4j-log4j12-1.6.4
log4j-1.2.16
Thanks in advance!
Log4jAmqBridge depends on the JMS API.
Add this dependency to your pom.xml :
<dependency>
<groupId>javax.jms</groupId>
<artifactId>jms</artifactId>
<version>1.1</version>
<scope>provided</scope>
</dependency>
It should solve your problem.

Cannot find the main class after converting java project to maven

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.

Categories