I want to execute a Java class from a bat file and it include usage of opencsv.jar.
Below is the code:
#echo off
set path=%PATH%;C:\Program Files (x86)\Java\jdk1.6.0\bin
javac -cp opencsv.jar ArbitrageUsingThread.java
java ArbitrageUsingThread
I am getting the following error when running it. I think there is some problem with including opencsv.jar Can anyone help me with this?
Error
Exception in thread "main" java.lang.NoClassDefFoundError: au/com/bytecode/openc
sv/CSVWriter
at ArbitrageUsingThread.main(ArbitrageUsingThread.java:67)
Caused by: java.lang.ClassNotFoundException: au.com.bytecode.opencsv.CSVWriter
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
java is similar to javac for classpath.
Under linux:
java -cp opencsv.jar:. ArbitrageUsingThread
Under windows:
java -cp opencsv.jar;. ArbitrageUsingThread
Related
I have a java program that uses dynamic reports fine in netbeans, and even works in exe and jar when I first run it. However, if I share the jar/exe (I've even sent it and re-downloaded it on my own computer), I get an error java.lang.classnotfoundexception: net.sf.jasperreports.engine.jrdatasource. However, I have double checked my jars and I do have this class. What could be the source of this problem then?
Thanks
Edit: full error:
Exception in thread "main" java.lang.NoClassDefFoundError:
net/sf/jasperreports/engine/JRDataSource
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:
net.sf.jasperreports.engine.JRDataSource
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
if you get Caused by: java.lang.ClassNotFoundException: inside a jar file then, you need to provide the classpath arguments;
example:
java -cp my.jar -jar ExternalJarFileName.jar
you can also provide all jars use below command, only need to create a directory and put all external jars in to it and use path of directory in to below command:
java -cp my.jar;C:\externalJar\* mainpackage.MainClass
When I give the following command
java -Xmx500m -jar bc.jar %1 %2 %3 %4 %5 %6 %7 %8 %9
I am getting the following Exception
Throwable Error: java.lang.NoClassDefFoundError: org/supercsv/prefs/CsvPreference
Exception in thread "main" java.lang.Error: java.lang.NoClassDefFoundError: org/supercsv/prefs/CsvPreference
at bc.c.Util.process(Util.java:135)
at bc.m.TabularReader$CSVReader.<init>(TabularReader.java:68)
at bc.m.TabularReader.<init>(TabularReader.java:37)
at bc.c.Spread2db.main(Spread2db.java:244)
Caused by: java.lang.NoClassDefFoundError: org/supercsv/prefs/CsvPreference
at bc.m.TabularReader$CSVReader.<init>(TabularReader.java:64)
... 2 more
Caused by: java.lang.ClassNotFoundException: org.supercsv.prefs.CsvPreference
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 guess that this is due to the improper inclusion of supercsv.
Can anybody help me fixing this?
Sounds like Super CSV isn't on the classpath, and as you're not supplying the classpath with -cp I would check the manifest in your jar to see where it expects the super-csv jar to be (probably in the same directory), then confirm that it's actually there.
This seems to be a fairly good overview of the different ways of configuring the classpath when running jars.
I have a batch file which runs on XP:
#echo off
#setlocal
set CWD=%~dp0
set JYTHON_HOME=%CWD%
set LIB_DIR=%JYTHON_HOME%/lib
java -cp %LIB_DIR% -jar %LIB_DIR%/jython.jar -Djython.home=%CWD%
-Dpython.path=%LIB_DIR%;%CWD%/ext %LIB_DIR%clenotes/clenotes.py %*
#endlocal
I want to run this file on Windows 7.
EDIT:
When I run it this error occurs:
Exception in thread "main" java.lang.NoClassDefFoundError:
folder\CLENotes\lib Caused by: java.lang.ClassNotFoundException:
folder\CLENotes\lib
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)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)
Actually it is very simple, I needed to change the fowardslashes (/) to backslashes.
after export from eclipse i have error:
C:\Program Files\Java\jre6\bin>java C:\wamp\www\JOGL\test.jar
Exception in thread "main" java.lang.NoClassDefFoundError: C:\wamp\www\JOGL\test/jar
Caused by: java.lang.ClassNotFoundException: C:\wamp\www\JOGL\test.jar
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)
Could not find the main class: C:\wamp\www\JOGL\test.jar. Program will exit.
manifest from this file:
Manifest-Version: 1.0
Sealed: true
Main-Class: windows.SimpleScene
the same error when export with Fat Jar
updated:
C:\Program Files\Java\jre6\bin>java -jar C:\wamp\www\JOGL\test.jar
Exception in thread "main" java.lang.NoClassDefFoundError: javax/media/opengl/GLEventListener
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(Unknown Source)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$000(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)
Caused by: java.lang.ClassNotFoundException: javax.media.opengl.GLEventListener
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)
... 12 more
Could not find the main class: windows.SimpleScene. Program will exit.
To run a jar file, you run:
java -jar (name-of-jar-file)
So you should run:
java -jar C:\wamp\www\JOGL\test.jar
Make sure, when you are exporting and creating you JAR three things are set:
Export it as a "runnable jar file".
Define your launch configuration (which is your class that has the main method in it).
select "Package Required libraries into generated JAR".
The command you entered only "java file.jar" is used to run .class files, the right syntax to run jar is the "java -jar file.jar" that search your jar for the manifest and execute it.
You can also run it by adding in the class-path and the main class name
java -cp C:\wamp\www\JOGL\test.jar com.ext.Example
That's not how you run a JAR file!
Use java -jar test.jar syntax
The reason you are seeing this error is java is looking at your current working directory as the classpath. where as your JAR file is located in a different directory.
You have the following options:
1)
cd C:\wamp\www\JOGL
java -jar test.jar (as you defined the Main-Class in the manifest file, java would pick it from there).
Make sure your jar maintains the same directory structure as your package structure.
2) java -cp C:\wamp\www\JOGL\test.jar window.SimpleClass (pointing the classpath to your jar)
Note: If you have dependencies on other JARs/classes beyond this JAR file, they need to be part of the classpath (absolute/relative to current directory).
I just installed JGNAT on Windows (gnat-gpl-2010-jvm-bin.exe)
This is a sample code hello.adb from the included manual:
with Ada.Text_IO; use Ada.Text_IO;
procedure Hello is
begin
Put_Line ("Hello GNAT for the JVM.");
end Hello;
First, compile it: jvm-gnatmake hello.adb
jvm-gnatcompile -c hello.adb
jvm-gnatbind -x hello.ali
jvm-gnatlink hello.ali
Looks fine. So let's run it: java hello
Exception in thread "main" java.lang.NoClassDefFoundError: jgnat/adalib/GNAT_libc
at hello.main(hello.adb)
Caused by: java.lang.ClassNotFoundException: jgnat.adalib.GNAT_libc
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
Any idea why?
Here's the fix. You must set the classpath to the jar file containing GNAT_libc.class.
java -cp C:\GNATPRO\2010\lib\jgnat.jar;. hello