ClassNotFoundException found while loading classes from extension classloader using java - java

I am using processbuilder to run specific task in seperate JVM instance. i have two directory lib and conflictlib. lib directory i have added under classpath and conflictlib i have added in "java.ext.dirs" System property. `
here is my command:
java -Xmx64m -XX:+HeapDumpOnOutOfMemoryError -Dserver.home='/home/local/tomcat/webapps/ROOT/WEB-INF' -Djava.ext.dirs='/home/local/tomcat/webapps/ROOT/WEB-INF/conflictlib' -cp '/home/local/tomcat/webapps/ROOT/WEB-INF/lib' SubProcessClass
why i have added conflictlib in java.ext.dirs system property-
i have some conflict jar files(same jar with different version) in lib and conflictlib. Task i need to perform in subprocess, classes should be loaded from conflictlib directory. lib directory i have added under -cp which will be loaded by System class loader and conflictlib is loaded by extension classloader. since extension class loaded first. but I am getting ClassNotFoundException.
java.lang.NoClassDefFoundError: org/json/JSONArray
Caused by: java.lang.ClassNotFoundException: org.json.JSONArray
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:581)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
json-20140107.jar
present under conflictlib directory. its giving ClassNotFoundException. could someone check this. i am not able to understand where i am doing mistake.
thanks in advance.

Related

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.

Runtime execution (JAR) cannot locate Java CLASSPATH and Connector J

before now, my application has always compiled and executed. I just do my compilation using
jar cvmf mainClass.txt starcoder.jar .class images/.gif images/*.jpg *.bat
I have a
**Windows 7 OS,
Java JDK jdk1.7.0_05,
JRE JRE7,
Connector J mysql-connector-java-3.1.14-bin,
Using a TextPad for development,
The runtime is on C:/Program Files/Java and Java_home**
This application from TextPad, it runs well with the CLASSPATH and executes fine.
The system compiles properly but my JAR file fails to execute
PROBLEM: It fails to find my CLASSPATH on runtine execution(which works perfectly on compile time) giving me this Error
c:\Program Files (x86)\Java\jdk1.7.0_05\bin>java -jar starcoder.jar
ClassNotFoundException: com.mysql.jdbc.Driver SQLException: No
suitable driver found for jdbc:mysql://localhost/starinv?user=r oot
Exception in thread "main" java.lang.NoClassDefFoundError:
javax/mail/MessagingE xception
at serialz.(serialz.java:96)
at serialz.main(serialz.java:240) Caused by: java.lang.ClassNotFoundException: javax.mail.MessagingException
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinCla
ssLoader.java:583)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(C
lassLoaders.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
... 2 more
The CLASSPATH
.;C:\Program Files (x86)\Java\jre7\lib\ext\comm.jar;.;C:\Program Files
(x86)\Java\jre7\lib\ext\mysql-connector-java-3.1.14-bin.jar;.;C:\Program
Files (x86)\Java\jre7\lib\ext\jxl.jar;.;C:\Program Files
(x86)\Java\jre7\lib\ext\mailapi.jar;.;C:\Program Files
(x86)\Java\jre7\lib\ext\mail.jar;.;C:\Program Files
(x86)\Java\jre7\lib\ext\activation.jar;.;C:\Program Files
(x86)\Java\jre7\lib
My connector J file is in the right location but I get an error while executing my JAR file.
I need help to resolve this.
is the same for both my development and runtime environment.
Try calling like this:
java -cp .;"C:\Program Files (x86)\Java\jre7\lib\ext\comm.jar";"C:\Program Files (x86)\Java\jre7\lib\ext\mysql-connector-java-3.1.14-bin.jar";"C:\Program Files (x86)\Java\jre7\lib\ext\jxl.jar";"C:\Program Files (x86)\Java\jre7\lib\ext\mailapi.jar";"C:\Program Files (x86)\Java\jre7\lib\ext\mail.jar";"C:\Program Files (x86)\Java\jre7\lib\ext\activation.jar";"C:\Program Files (x86)\Java\jre7\lib" -jar starcoder.jar
Note that I removed your classpath's redundant "." entries and quoted each path.

Java SQLite org.sqlite.JDBC classpath broken?

I stumbled upon a weird error while using JDBC sqlite with org.sqlite.JDBC
my code compiles and runs fine on Windows.
But when I tried moving it to Ubuntu it started showing this:
Exception in thread "main" java.lang.ClassNotFoundException: org.sqlite.JDBC
at java.net.URLClassLoader$1.run(URLClassLoader.java:372)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:259)
at mall.SQLiteJDBC.<init>(SQLiteJDBC.java:27)
at mall.AllegroReader.<init>(AllegroReader.java:33)
at mall.Mall.main(Mall.java:31)
I'm running it with java -classpath "sqlite-jdbc-3.7.2.jar" -jar Mall.jar" and java -classpath "sqlite-jdbc4-3.8.2-SNAPSHOT.jar" -jar Mall.jar
with both versions in the same directory as my jar and I've tried a dozen different options specifying classpath and it behaves exactly the same. I tried openjdk and oracle jdk.
I tried rebuilding it on Ubuntu, changing ant .xmls, changing paths, etc.
I have no idea what is going on. Pls help.
Here is what happens inside my dist directory:
work1#workwork:/var/www/mall/dist$ ls
mall.db Mall.jar Mall.jar.old sqlite-jdbc-3.8.4.3-SNAPSHOT.jar
work1#workwork:/var/www/mall/dist$ java -classpath "sqlite-jdbc-3.8.4.3-SNAPSHOT.jar:Mall.jar" Mall
Error: Could not find or load main class Mall
The classpath is ignored when you use -jar.
You have to either include the dependencies in the jar (or at least have the jar manifest point to them), or run it with -classpath sqlite.jar:Mall.jar the.main.class.
Error: Could not find or load main class Mall.main. all files are there,
my main class comes from Mall.java and is in mall package which
compiles to Mall.jar
So the correct command line is:
java -classpath "sqlite-jdbc-3.8.4.3-SNAPSHOT.jar:Mall.jar" mall.Mall
OP findings
to view the classes in jar use jar tf Mall.jar - from this I got mall/Mall.class meaning my class containing main was mall.Mall
it showed
mall/Mall.class
so I should have used mall.Mall as the class to run (instead of pulling my hair)
After spending over 6 hours total with many failed attempts at running "portable" jar package using classpath and whatnot, after having tried OneJar and jarjar to no avail (ended up with Class file too large!) I decided to write the offending piece of code in PHP.
It proved to be more portable than Java in my case.

Could not find the main class

I am trying to run a java program bundled in the jar named easyflow-gui.jar using cmd:
java -classpath "." -jar easyflow-gui.jar
The working directory is the directory containing all relevant libraries.
The content of the Manifest file of the jar file I am trying to run is:
Manifest-Version: 1.0
Ant-Version: Apache Ant 1.8.3
Created-By: 1.6.0_32-b27 (Sun Microsystems Inc.)
Main-Class: easyflow.custom.jgraphx.editor.SchemaEditor
The result of this attempt is:
Exception in thread "main" java.lang.NoClassDefFoundError: com/mxgraph/util/mxEventSource$mxIEventListener
Caused by: java.lang.ClassNotFoundException: com.mxgraph.util.mxEventSource$mxIEventListener
at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
Could not find the main class: easyflow.custom.jgraphx.editor.SchemaEditor. Program will exit.
Question: which one of the classes is actually not found: the mxEventSource$mxIEventListener or the main class easyflow.custom.jgraphx.editor.SchemaEditor ?
Edit 1:
I checked the folder and extracted the jars and I find both classes available (bundled into its respective jars in the working dir):
$ls easyflow/custom/jgraphx/editor/SchemaEditor*
easyflow/custom/jgraphx/editor/SchemaEditor$1.class
easyflow/custom/jgraphx/editor/SchemaEditor$2.class
easyflow/custom/jgraphx/editor/SchemaEditor.class
easyflow/custom/jgraphx/editor/SchemaEditor.java
$ls com/mxgraph/util/mxEventSource*
com/mxgraph/util/mxEventSource$mxIEventListener.class
com/mxgraph/util/mxEventSource.class
You cannot specify "." for the classpath if you want to include a jar file, it has to be a colon-separated list of jar files or directories (or semicolon-separated, depending on the operating system). Try java -help to get a description of the command line options.
Also if I remember correctly -jar and -classpath do not work together, so you have to use -classpath alone and specify the main class explicitly.
Try something like
java -cp easyflow-gui.jar:foo.jar:bar.jar easyflow.custom.jgraphx.editor.SchemaEditor
... where foo.jar and bar.jar are the "other relevant libraries".
If you read the exception properly, you will see that the cause of the error:
Caused by: java.lang.ClassNotFoundException: com.mxgraph.util.mxEventSource$mxIEventListener
You didn't include the jar file that contains the com.mxgraph.util.mxEventSource$mxIEventListener in your classpath. From a quick Google search, you will need jGraph library.
I hope this helps.
The default classpath is the current working directory.
So, if you already have the jar in your current working directory, you dont need to specify the classpath explicitly.
The following command should work
java -jar easyflow-gui.jar

How to correctly make a java jar file using this makefile with files in src/ and bin/

I am having trouble correctly generating a Jar for my Java implementation. I am not a Java master ( Thus why I am using Make and not Ant). I didn't think I cared about a Jar until I realized my Windows machine didn't hava javac. Then I thought to myself "If only I had a jar"
So I hacked a few lines together in a Makefile trying
The relevant lines are
all: client server jar
client: bin bin/Job.class bin/JobQueue.class bin/Client.class bin/FileTransfer.class
server: bin bin/Job.class bin/JobQueue.class bin/Server.class bin/ServerThread.class bin/FileTransfer.class
jar: client server
jar cfe HBNQServer Server bin/Server.class bin/Job.class bin/JobQueue.class bin/ServerThread.class bin/FileTransfer.class
jar cfe HBNQClient Client bin/Client.class bin/Job.class bin/JobQueue.class bin/FileTransfer.class
However when I attempt to run it I get a No Class Found Exception.
$ java -jar HBNQServer
Exception in thread "main" java.lang.NoClassDefFoundError: Server
Caused by: java.lang.ClassNotFoundException: Server
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:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
I am sure the mistake is painfully obvious to anyone more familiar with "Jaring" files.
If I left any important code out, it may be found Here
I believe your problem is the bin prefix. If you jar a class file, it must be in a directory identical to its fully qualified package name. For example, if you implement a com.mycompany.Server class, the corresponding path in the jar file must be com/mycompany/Server.class. In your case, everything is in the bin directory in the jar file, so it can't find your (presumably un-package-qualified) classes. Either put all of your classes in the bin package, or jar them up without the bin prefix.
Something like this might work:
jar: client server
cd bin; jar cfe ../HBNQServer Server Server.class Job.class JobQueue.class ServerThread.class FileTransfer.class
cd bin; jar cfe ../HBNQClient Client Client.class Job.class JobQueue.class FileTransfer.class
You need a manifest for your JAR, as discussed here.

Categories

Resources