call java program which includes jna from batch file - java

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

Related

Running jar file from cmd with reference to external jars

I would like to know can I run a jar file from the command, with the jar file using log4j and ojdbc.jar as well.
The 'main' is located in: nmap_logic.jar.
Within the package containing the 'main' is called: "nn.gmap.logic".
I also use 2 external jar files: log4j.jar & ojdbc.jar.
I have tried running:
java -cp "nmap_logic.jar;log4j.jar;ojdbc.jar" nn.gmap.logic.NNmain
And I get an error that the log4j cannot be initialized.
From the Eclipse environment the application runs fine.
Please let me know how should I execute the command properly.
Thanks.
Try to give the full path to the jars. I believe that there is a difference between what you think is your root folder and what Java thinks about it.
Something like java -cp "c:\myjars\nmap_logic.jar;c:\myjars\log4j.jar;c:\myjars\ojdbc.jar" nn.gmap.logic.NNmain
Btw, you can also do the following: java -cp "c:\myjars\*" nn.gmap.logic.NNmain

JAVA, Batch file errors

I have an existing Java project that compiles and runs properly through Eclipse. I have created the following .bat file to run the program sans Eclipse:
java -classpath jflashplayer.jar;bin TestProgram
The file is saved within the project folder, but not within the bin folder (located in same directory as bin). When I try to run the batch, I am met with a large number of runtime errors, the first being
Exception in thread "AWT-EventQueue-0" java.lang.NoClassDefFoundError: org/apache/commons/io/FileUtils
I'm not sure why I get this error when it compiles and runs properly via Eclipse. I have the commons-io jar files linked to the project within Eclipse as libraries, and the jar files are themselves located in the project file (same directory as the batch file and the bin folder).
Also, I'm not entirely sure what the -classpath jflashplayer.jar bit of the batch file is doing. I am using the jflashlayer.jar library (also linked to the project within Eclipse and in the same location as the other jar files), but I am not sure why it would appear in the batch file. I edited an existing batch file from a similar project that uses the jflashplayer.jar files, and it has worked previously to leave that part in.
When I write code in Java, I rarely require it to compile/run outside of the IDE, so I usually have troubles when it comes to this part. Perhaps there is a more robust and foolproof method to run the program outside of the IDE other than the batch file method.
The batch file method is fine, but you have to specify all the libraries you're using on the classpath, just like the jflashplayer.jar.
In this case, the error you're getting is because the Apache commons-io library is not specified on the classpath. Your command would have to look something like:
java -classpath jflashplayer.jar;commons-io.jar;<other jars ...>;bin TestProgram
Alternatively, you can create a runnable jar from Eclipse as described here. When you select a library handling strategy, choose the option Extract required libraries into generated JAR. This will make it so that all the library classes you're using are packaged into your application's jar file, and you can just execute it by invoking java -jar my_application.jar.

java.lang.NoClassDefFoundError with jar files

Uploaded a jar file from my computer to a server and tried to run it. When I run it I get java.lang.NoClassDefFoundError and it seems to be related to the twitter4j jar that my main method depends on.
However, I have this jar file in my libraries so shouldn't this be included when I build my code in to a jar? Here's a pic in case it helps.
is the error that I'm getting. (can't upload a pic just yet.
Not sure what this has to do with twitter, but anyway, the issue is that you do not have the correct class files. In other words, when you are running your fat JAR in the command prompt, you do not have any libraries exported with it (Or if you do, you don't have that specific one).
Sometimes such an error can be because there is an incorrect version of java, however that is not the case here since java has got no "twitter" packages or classes in it.
Using something like JarSplice would fix this.
Assuming you did not package the twitter4j classes inside your application jar, you need to tell Java where it can look for classes that are not inside your application jar. You typically use the classpath flag for that. In your case, it should look something like
java -cp /tmp/twitter4j.jar -jar /tmp/myapp.jar
An alternative would be to package all twitter4j's classes inside your application jar. This is called a 'fat' jar. How to make one depends on how you build your application jar.
The JAR file that you are trying to use needs to be in the classpath. This can be done by using the -cp attribute from the command line. However, when using java -jar, you cannot use the -cp attribute.
To get around this, you can do the following:
java -cp /tmp/myapp.jar;\path\to\external.jar com.example.package.MyClass
where MyClass has the main() method defined.
Alternately, you can specify jar files on the classpath using the manifest.mf file. See http://docs.oracle.com/javase/tutorial/deployment/jar/manifestindex.html for details.

Running java from a bat file in such a way that path doesn't need to be specified

I think I have seen this done, but am not sure where. What I want to do is to create a bat file I can package with my class files when sending to a friend to show them progress/ask advice on non programming matters. My friend is not very handy when it comes to code and doesn't like changing computer settings. Just using java myClass as a command line won't work here because although my friend does have java installed, he has not set his windows environment variables so his command prompt knows where to find java.
What kind of line would I need to add to my batch file to make it so it can compensate for problems like this?
Create a manifest file (manifest.txt):
Main-Class: com.mycompany.myapp.MyMainClass
Package your app as a jar:
jar cfm myjarfile.jar manifest.txt *.class
Create a batch file:
start myjarfile.jar
If it is about sharing and running a single java file without jar dependencies. And you are only worried about the java runtime environment setup, then you can use online java code compilers and executors. Here is one:
http://javalaunch.com/JavaLaunch.jsp
You can google for more!
Use an IDE, NetBeans or eclipse and package your files as a Jar file.. that can be executed directly and you do not need to worry about dependencies, other classes or libraries.

.jar file only works when used in Eclipse

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.

Categories