I'm trying to run a class that I wrote that is importing the jawin library.
My command line is as below:
java -cp C:\class_path ClassName
I'm getting:
Exception in thread 'main' java.lang.NoClassDefFoundError org/jawin/funcptr
What am I doing wrong?
Doesn't the JAR that I'm adding to the build path get into the binary file that I've compiled?
If not, then when I publish my class file how will it know to find the external JAR? What if it doesn't exist on the target computer?
Related
I am new to Java and JNA. I was trying to access the native interfaces of a third party applications using JNA from Java eclipse. So I was trying to set the jna.library.path from Command line using the command
java -Djna.library.path=<path to your library> MainClass
I have given the "path to your library" as the path of the folder where my .dll file and its dependencies are there. for ex: D:Adrin\Library\Dll.
"MainClass" I have given the name of the class with main() in my application(Jnaexe.java). But an error occurs saying
Error: Could not find or load main class Jnaexe.java
I have also tried by giving the package name and also the whole path of the Jnaexe.java. Still it does't work. I tried running from the src folder and even the folder where Jnaexe.java file is there(example folder) in command prompt. But the error was same.What I am missing here. Any help will be highly appreciated. Thank you in advance
I have created an application that uses selenium-server-standalone-2.47.1.jar and javax.mail.jar. The code works on eclipse, but I would like to run the same from command line. So I exported the project to a runnable jar file which contains both selenium and javax.mail.jar.
My code contains RTC.java which has the Main function and another Ex.java.
Both the class files are generated in com folder.
My App1.jar file is located in C:\installers.
I used the command:
c:\installers> java -cp App1.jar com.RTC
It says:
Exception in thread "main" java.lang.NoClassDefFoundError: org.openqa.selenium.WebDriver
Further I used:
java -cp .App1.jar com.RTC
Then it says could not find or load main class com.RTC.
What am I doing wrong?
I used the command:
c:\installers> java -cp App1.jar com.RTC
It says:
Exception in thread "main" java.lang.NoClassDefFoundError:
org.openqa.selenium.WebDriver
That exception usually means that a .class file was found but it didn't contain the right class. Check how you're putting it into the JAR. It's directory and file name must match its package and class name.
It sometimes also seems to mean that a secondary class wasn't found. Normally secondary JAR files are mentioned in the class-path entry of the manifest of the main JAR file, along with the main-classname, so you can use
java -jar App1.jar
Further I used:
java -cp .App1.jar com.RTC
Then it says could not find or load main class com.RTC.
I'm not surprised. If the first command got as far as it did, the JAR filename doesn't start with a dot. Cannot imagine why you tried this. It is nonsense.
I found the solution. I need not create a jar file which has the reference jars. I have to mention my jar which has my code and the reference jars that I am using in the class path.
My code is in App.jar.The reference jars are
selenium-server-standalone-2.47.1.jar and
javax.mail.jar.
so I used
c:\installers> java -cp selenium-server-standalone-2.47.1.jar;javax.mail.jar;App.jar com.RTC
Thank You.
I am trying to call a java program in a batch file. My java program calls a dll with JNA. When running batch file, it says it cannot find jna library class. I have already put jna jar files to my batch file folder. What can be the missing point?
My batch file:
#ECHO OFF
java -cp .;jna-4.1.0.jar com/sun/jna/Library
java MyBenchmark
I am getting below error:
java.lang.NoClassDefFoundError: com/sun/jna/Library
My jna jar file also same folder with my benchmark batch file too.
When use java -cp .;jna-4.1.0.jar com/sun/jna/Library, cmd gives that error also:
Main method not found in class com.sun.jna.Library
Library is an interface and my called java program uses that interface. But cmd says it cannot load it without main. I have to use it for reaching jna.
You must specify the ClassPath "-cp": java -cp [path to your jar] [class file to run]
Check this
If you use JNA and want to call a java program which use its library from batch file, you must avoid using java file. Because it can not load classes at runtime when you call jna.jar from batch too.
My code was like:
#ECHO OFF
java -jar jna-4.1.0.jar
java MyBenchmark
I tried many other solution too but nothing works(I also used code at question part too).
Lastly I tried export my project as jar and used it. First of all I want to state that my IDE was Intellij IDEA. It cannot put project dependency to my manifest file(jna path) although I put jar dependency already. So if you use IDEA, you must enter that from artifact screen manually, otherwise your jar cannot work right. Then you can put your jar under same folder with your bacth and call it like below:
java -jar MyJarFile.jar
I'm using a .jar file which I imported to Eclipse, and when I used Eclipse to run the application is works just fine.
But when I try running it from the command line with just the command java ClassName I get a NoClassDefFoundError message.
The .jar file is in the same directory as the main class I need to run.
I tried using the classpath command in the terminal
java -classpath pack.jar ClassName
but I got the same error but this time it was in the main thread
Exception in thread "main" java.lang.NoClassDefFoundError
Why does my program work in Eclipse but not on its own?
I guess you'll need to use: java -classpath .:pack.jar ClassName.
NoClassDefFoundError will come if a class was present during compile time but not available in java classpath during runtime.
Its Not Even Close To Being equal to ClassNotFoundException n u mustn't confuse them together, so u need to include the whoe path of the .jar file,
use the previously explained technique to do so, or if it was a jar file that want use frequently without using the call to java -classpath .. thing try importing the file into the OS class path directory.
I have created a jar file usign maven2 build. I am trying to run that jar file using the command:
java -jar sample.jar com.app.Test
Test being the class which is having the main method. But i am getting this exception:
Exception in thread "main" java.lang.NullPointerException
at sun.launcher.LauncherHelper.getMainClassFromJar(Unknown Source)
at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)
Can any one help me to solve this exception and run the jar file?
Thanks in advance.
If you want to run the Test class, you should use
java -cp sample.jar com.app.Test
This way, you add the jar to the classpath and then run the specified main class.
What java -jar does is that it executes a runnable jar file (which defines its own main class in the manifest file). Any parameters after that would not be used to specify the class, but end up in the String array passed to the main method.
So if you have a properly constructed runnable jar file, it should just be
java -jar sample.jar
If you are using Maven, you may need to use maven's install command. You can find the format here
http://maven.apache.org/guides/mini/guide-3rd-party-jars-local.html
Looks like you do need a manifest and a Main-Class attribute, if you don't have one.
If you are using java 7 a bug is filed here http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=7067922