Java class not found in Jackson - java

I'm new to Java and have a program that's throwing the following error:
Exception in thread "main" java.lang.NoClassDefFoundError: org/codehaus/jackson/map/ObjectMapper
at ExtractCustomerIds.main(ExtractCustomerIds.java:21)
Caused by: java.lang.ClassNotFoundException: org.codehaus.jackson.map.ObjectMapper
at jdk.internal.loader.BuiltinClassLoader.loadClass(java.base#9-internal/BuiltinClassLoader.java:366)
at jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(java.base#9-internal/ClassLoaders.java:184)
at java.lang.ClassLoader.loadClass(java.base#9-internal/ClassLoader.java:419)
... 1 more
I know the problem has to be with paths, classpath or jar name, but I can't figure out as of now.
My folder structure is:
.
- get_customer_ids
- ExtractCustomerIds.java
- libs
- jackson-all-1.9.11.jar
So when I'm inside the get_customer_ids I give the following command:
get_customer_ids$ javac -cp ../libs/* ExtractCustomerIds.java and this causes the error above.
I thought I was setting the classpath correctly, but it's still not working. What's missing?

The exception occurs when running your program - you are specifying the classpath while compiling. The classpath set during compilation is not stored inside the compiled class files or the like - you need to correctly specify it when running your program, too!
Like so:
$ java -cp ../libs/* ExtractCustomerIds

Related

Execute certain java class which is not main class of a jar file from linux

I need to execute a java class in a jar file. I tried as follows:
java -jar MyJar.jar
And got: no main manifest attribute, in MyJar.jar.
Then I realize that I must run a certain class which is in /directoryInsideJar/MyClass.class, but I don't have permission to edit MANIFEST.MF file. I found that I directly could theoretically run it by:
java -cp MyJar.jar firstdirectoryinpath.path.MyClass
But I got:
Exception in thread "main" java.lang.NoClassDefFoundError: scala/collection/Seq (...)
Caused by: java.lang.ClassNotFoundException: scala.collection.Seq
(...)
I'm pretty sure that the class exists and the path is correct.
What I'm doing wrong?

how to run a java file from a parent directory?

I am able to compile a java file that is inside a directory by using javac <directory_name>/Solution.java.
but I am not able to run the java program in the same manner ie. java /Solution.
as it gives me this error :
Error: Could not find or load main class code.Solution Caused by:
java.lang.NoClassDefFoundError: Solution (wrong name: code/Solution)
Here code is my directory name.
I could cd into the directory and do it. which actually works but that is not the behavior that I desire.
How to achieve this?

TestRig / grun drops java.lang.NoClassDefFoundError when using the output directory -o option

Following this post, I am using the following steps to compile the parser/lexer from this repository:
export CLASSPATH=".:/usr/local/Cellar/antlr/&ltversion&gt/antlr-&ltversion&gt-complete.jar:$CLASSPATH"
antlr &ltgrammarName&gt.g4 -o &ltsomeFolder&gt/
javac &ltsomeFolder&gt/&ltgrammarName&gt*.java
but when I use the instructions here:
grun <someFolder>/<grammarName> tokens -tokens < <inputFile>
I get this error messages:
Exception in thread "main" java.lang.NoClassDefFoundError: IllegalName: &ltsomeFolder&gt/&ltgrammarName&gtLexer
at java.base/java.lang.ClassLoader.preDefineClass(ClassLoader.java:889)
at java.base/java.lang.ClassLoader.defineClass(ClassLoader.java:1014)
at java.base/java.security.SecureClassLoader.defineClass(SecureClassLoader.java:151)
at java.base/jdk.internal.loader.BuiltinClassLoader.defineClass(BuiltinClassLoader.java:825)
at java.base/jdk.internal.loader.BuiltinClassLoader.findClassOnClassPathOrNull(BuiltinClassLoader.java:723)
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClassOrNull(BuiltinClassLoader.java:646)
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:604)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:168)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
at org.antlr.v4.gui.TestRig.process(TestRig.java:129)
at org.antlr.v4.gui.TestRig.main(TestRig.java:119)
I would appreciate if you could help me know what is the problem and how I can resolve it.
I don’t see where you’ve specified a package name, so now your Java classes are located in <someFolder>. Be sure to compile them in that folder.
Then you’ll need to add that folder to your classpath (probably instead of “.”)
Try adding <someFolder> into the CLASSPATH you’re exporting. Then leave it off of your grun command line.
Java will only load classes from the Classpath (it’s a security thing). When TestRig runs, it attempts to load your class by building the Java class name it would have produced for you Parser (and Java will have to find that class somewhere in the classpath).
Your could modify the grun alias to allow for you to specify a directory to search for your classes, and use the -cp option on the Java command, but that’s probably more trouble than just adding it to you classpath that you’re using for this testing.

Mac JDK11: Exception in thread "main" java.lang.NoClassDefFoundError: java/sql/SQLException

New to Java here. Using IntelliJ. I wrote a simple "helloworld" Spring application that compiles just fine, but when I run it receive the following error:
/Library/Java/JavaVirtualMachines/jdk-11.0.1.jdk/Contents/Home/bin/java "-javaagent:/Applications/IntelliJ IDEA.app/Contents/lib/idea_rt.jar=51884:/Applications/IntelliJ IDEA.app/Contents/bin" -Dfile.encoding=UTF-8 -p /Users/winston.kotzan/Development/java-spring-helloworld/bin:/Users/winston.kotzan/java/spring-framework-5.1.3/libs/spring-aop-5.1.3.RELEASE.jar:/Users/winston.kotzan/java/spring-framework-5.1.3/libs/spring-aspects-5.1.3.RELEASE.jar:/Users/winston.kotzan/java/spring-framework-5.1.3/libs/spring-beans-5.1.3.RELEASE.jar:/Users/winston.kotzan/java/spring-framework-5.1.3/libs/spring-context-5.1.3.RELEASE.jar:/Users/winston.kotzan/java/spring-framework-5.1.3/libs/spring-context-indexer-5.1.3.RELEASE.jar:/Users/winston.kotzan/java/spring-framework-5.1.3/libs/spring-context-support-5.1.3.RELEASE.jar:/Users/winston.kotzan/java/spring-framework-5.1.3/libs/spring-core-5.1.3.RELEASE.jar:/Users/winston.kotzan/java/spring-framework-5.1.3/libs/spring-expression-5.1.3.RELEASE.jar:/Users/winston.kotzan/java/spring-framework-5.1.3/libs/spring-instrument-5.1.3.RELEASE.jar:/Users/winston.kotzan/java/spring-framework-5.1.3/libs/spring-jcl-5.1.3.RELEASE.jar:/Users/winston.kotzan/java/spring-framework-5.1.3/libs/spring-jdbc-5.1.3.RELEASE.jar:/Users/winston.kotzan/java/spring-framework-5.1.3/libs/spring-jms-5.1.3.RELEASE.jar:/Users/winston.kotzan/java/spring-framework-5.1.3/libs/spring-messaging-5.1.3.RELEASE.jar:/Users/winston.kotzan/java/spring-framework-5.1.3/libs/spring-orm-5.1.3.RELEASE.jar:/Users/winston.kotzan/java/spring-framework-5.1.3/libs/spring-oxm-5.1.3.RELEASE.jar:/Users/winston.kotzan/java/spring-framework-5.1.3/libs/spring-test-5.1.3.RELEASE.jar:/Users/winston.kotzan/java/spring-framework-5.1.3/libs/spring-tx-5.1.3.RELEASE.jar:/Users/winston.kotzan/java/spring-framework-5.1.3/libs/spring-web-5.1.3.RELEASE.jar:/Users/winston.kotzan/java/spring-framework-5.1.3/libs/spring-webflux-5.1.3.RELEASE.jar:/Users/winston.kotzan/java/spring-framework-5.1.3/libs/spring-webmvc-5.1.3.RELEASE.jar:/Users/winston.kotzan/java/spring-framework-5.1.3/libs/spring-websocket-5.1.3.RELEASE.jar -m SpringHelloWorld/com.wakproductions.MainApp
Exception in thread "main" java.lang.NoClassDefFoundError: java/sql/SQLException
at spring.core#5.1.3.RELEASE/org.springframework.core.Constants.<init>(Constants.java:67)
at spring.beans#5.1.3.RELEASE/org.springframework.beans.factory.xml.XmlBeanDefinitionReader.<clinit>(XmlBeanDefinitionReader.java:102)
at spring.context#5.1.3.RELEASE/org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:83)
at spring.context#5.1.3.RELEASE/org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:133)
at spring.context#5.1.3.RELEASE/org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:622)
at spring.context#5.1.3.RELEASE/org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:518)
at spring.context#5.1.3.RELEASE/org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:144)
at spring.context#5.1.3.RELEASE/org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:85)
at SpringHelloWorld/com.wakproductions.MainApp.main(MainApp.java:8)
Caused by: java.lang.ClassNotFoundException: java.sql.SQLException
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:583)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
... 9 more
I suspect that it may have to do with the CLASSPATH. I tried setting the CLASSPATH environment variable, as well as adding this to the java command
-cp /Library/Java/JavaVirtualMachines/jdk-11.0.1.jdk/Contents/Home
But that does not seem to work. Might I have the incorrect CLASSPATH?
The comments about the module-info file led me to some clues. I took notice that IntelliJ seems to have created a module-info.java file with the following contents:
module SpringHelloWorld {
requires spring.context;
}
Did not realizing I was compiling a module. I deleted that file along with everything in the bin directory. Then I was able to run it by the Run menu using this option:
I can run it on the command line via:
java -Dfile.encoding=UTF-8 -classpath /Users/.../java-spring-helloworld/bin:/Users/.../<spring-framework-libs>... com.wakproductions.MainApp

noclassdeffound error in java, linux

running a java class by shell script:
java -cp $CLASSPATH CG_GpsRequest "dbname","oracle.jdbc.driver.OracleDriver","abc","abc"
while running the script in unix, getting error,
Exception in thread "main" java.lang.NoClassDefFoundError: CG_GpsRequest
Caused by: java.lang.ClassNotFoundException:
CG_GpsRequest is the class file name.
Okay.. If I understand you correctly..
I think its problem with the Package name specification..
Your calss CG_GpsRequest you must have specified in Packaging way for example
com.xxx.yyy.CG_GpsRequest
So I think JVM is searching your class com.xxx.yyy.CG_GpsRequest in this patter so Please run your script with the following modifications.
java -cp $CLASSPATH com.xxx.yyy.CG_GpsRequest "dbname","oracle.jdbc.driver.OracleDriver","abc","abc"
This is just assumption that you have created your class in Pacakgin hirarechy since you havent specified more information.
Add the folder/path in which your java class exists to your CLASSPATH

Categories

Resources