Search code examples
javabatch-filejna

call java program which includes jna from batch file


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.


Solution

  • 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