Cannot call java -classpath jna.jar <class> - java

I used jna (java native access ) to compile and run C source in cmd environment.
Firstly, I created two files cSource.c and Example.java and put them in C drive.My jdk version is 7 (64bit), and window 7 64bit
//cSource.c
#include <stdio.h>
void printPointOf(int n) {
//printf("%p\n",&n);
}
and
//Example.java
import com.sun.jna.Library;
import com.sun.jna.Native;
import com.sun.jna.Platform;
public class Example {
public interface CSource extends Library {
public void printPointOf(int n);
}
static public void main(String argv[]) {
CSource cSrc = (CSource) Native.loadLibrary("csource", CSource.class);
int n = 1;
cSrc.printPointOf(n);
}
}
Secondly, i opened cmd, then compiled and run
Step 0. cd c:/
Step 1. gcc -o libcsourc.dylib -shared cSource.c
Step 2. javac -classpath jna.jar Example.java
Step 3. java -classpath jna.jar:. Example
three first steps were successful, but step 3 my cmd caught an error:
Could not find or load main class
How can i solve the above error? Is my command in step 3 wrong?
I consulted an article here : Running C in Java with JNA

Related

Loading a custom library with JNA on Windows

I have a .c file in which are defined methods with JNIEXPORT and i don't know how use these methods in a Java class importing them with JNA
I try to read this guide but I don't understand if it's possible to link a specific .c file.
Can someone help me?
Yes, it's possible, build and compile a shared library as you usually do and load it with Native.loadLibrary.
C:
#include <stdio.h>
void exampleMethod(const char* value)
{
printf("%s\n", value);
}
Compile it in the usual way (showing gcc on linux here):
gcc -c -Wall -Werror -fPIC test.c
gcc -shared -o libtest.so test.o
Java:
import com.sun.jna.Library;
import com.sun.jna.Native;
public class TestJNA{
public interface CLibrary extends Library {
public void exampleMethod(String val);
}
public static void main(String[] args) {
CLibrary mylib = (CLibrary)Native.loadLibrary("test", CLibrary.class);
mylib.exampleMethod("ImAString");
}
}
Since you are having issues finding the library, this is usually fixed configuring the java.library.path adding a new location where .so/.dll will be searched:
java -Djava.library.path=c:\dlllocation TestJNA
Alternatively you can set it directly from your code before loading the library (works with JNI, should work with JNA too, but i didn't try it):
String libspath = System.getProperty("java.library.path");
libspath = libspath + ";c:/dlllocation";
System.setProperty("java.library.path",libspath);
//Load your library here
Following the answer of "uraimo" for jna it should use jna.library.path instead of java.library.path and that should solve the location issue.

How to use a dll file in java code using JNA?

I have to use a dll file (of c code) in my java code, for which I searched allot and found that JNA is the most suitable way for it. Therefore, I am trying to write a HelloWorld program using jna-4.1.0.jar. Below is the c code:
//C hello world example
#include <stdio.h>
void __declspec(dllexport) _stdcall helloFromC()
{
printf("Hello world from the c code! \n");
}
and I make dll for this c code by using the following commands on cmd:
gcc -c ctest.c
gcc -shared -0 test.dll ctest.o
running these commands gives me a dll for the c code, which I place at the root of my simple java project.
Now here is my java class:
import com.sun.jna.Native;
import com.sun.jna.Library;
public class HelloWorld {
public interface CTest extends Library {
void helloFromC();
}
public static void main(String[] args) {
CTest INSTANCE = (CTest) Native.loadLibrary("ctest", CTest.class);
INSTANCE.helloFromC();
}
}
Now when I run this java program on eclipse, I am getting this error:
Exception in thread "main" java.lang.UnsatisfiedLinkError: %1 is not a valid Win32 application.
Kindly help me to solve this issue as it has already taken much of time!
gcc -c ctest.c
gcc -shared -o test.dll ctest.o
try 'o' ^ not '0'
It's finding your dll but it doesn't like it.

Exception in thread "main" java.lang.UnsatisfiedLinkError 3

Hi guys i have tried all the solutions like
java -Djava.library.path=. demo
adding the dll path to PATH
java -Djava.library.path=c:\JNI\demo.dll demo
But still the above error.
Here is my java code..
class demo
{
public native void printline();
public static void main(String[]args)
{
new demo().printline();
}
}
Here is my c code...
#include<stdio.h>
#include<jni.h>
#include "demo.h"
JNIEXPORT void JNICALL Java_demo_printline(JNIEnv *a, jobject b)
{
printf("Hello wrold!!!");
return;
}
Steps for compiling and running,
javac demo.java
javah demo
gcc -c -I"c:\jdk1.7.0_55\include" -I"c:\jdk1.7.0_55\include\win32" demo.c
gcc -Wl,--add-stdcall-alias -shared -o demo.dll demo.c
java -Djava.library.path=c:\JNI\demo.dll demo
Am i going wrong somewhere?
can someone please help me out,.
Try Run-Time Loading of the dll file within the java code in a static block like:
static
{
System.loadLibrary("demo");
}
should give you the output.
Moreover make sure that dll file generated is x32 or x64 according to the gcc compiler in use.
looking for "JNI hello world" (or many other terms, possibly), would have given you the answer.
for example:
http://www3.ntu.edu.sg/home/ehchua/programming/java/JavaNativeInterface.html
you need to load the library inside your java code
you need to specify the path to the directory of the library, not to the library itsself, in java.library.path

java c++ jni unsatisfied link error

I'm quite a newbie to Java JNI. I tried a helloCPP program but ran into problems. here's my code :
helloCPP.java;
public class helloCPP {
public native void hellocpp ();
static {
System.loadLibrary("helloCPP");
}
public static void main (String [] args) {
helloCPP hcpp=new helloCPP ();
hcpp.hellocpp ();
System.exit (0);
}
}
Then here's my hellocpp.cpp out of which I made helloCPP.dll ;
#include <iostream>
#include "helloCPP.h"
using namespace std ;
JNIEXPORT void JNICALL Java_helloCPP_hellocpp. (JNIEnv *env, jobject obj) {
cout <<"hello java, I'm c++\n";
}
int main (){};
I successfully built the helloCPP.dll. But when I try to run java helloCPP, I get this error:
Exception in thread "main"
java.lang.UnsatisfiedLinkError: helloCPP.hellocpp () V
at helloCPP.hellocpp(Native Method)
at helloCPP.main (helloCPP.java :8)
I'm doing all these stuff on a Windows 8 x86 operating system.
These are the commands I wrote:
javac helloCPP.java
javah helloCPP
g++ -c hellocpp.cpp
g++ -o helloCPP.dll hellocpp.cpp
This one generated the error:
java HelloCPP
Thanks in advance.
Most likely, the Java run-time cannot locate the DLL file. You have run the Java application with additional arguments (assuming the DLL file is in the current directory):
java -Djava.library.path=. HelloCPP
Update:
EJP has a good point. I think your link command isn't correct. The second g++ command should be something like:
g++ -o helloCPP.dll -shared hellocpp.o
There are two changes: hellocpp.o instead of hellocpp.cpp and more importantly the option -shared to indicate that you want to create a shared library and not an executable.
The exact options depend on your platform. As you haven't specified it, I can't tell you for sure.

Java class not running on OS X

I have a simple program written in Java:
package edu.oakland.lecture;
public class Alfa {
int a;
public int getAttribute() {
System.out.println("returning value of a");
return a;
}
public static void main(String []args) {
Alfa alfa = new Alfa();
int number = alfa.getAttribute();
System.out.println(number);
}
}
It compiles with javac on both windows (xp) and os x (lion), but it only runs on windows.
This is the command I use to compile the program:
javac -d bin source/edu/oakland/lecture/Alfa.java
This is the command I use to execute it:
java -classpath bin; edu.oakland.lecture.Alfa (I also tried -cp instead of -classpath in Terminal)
As I mentioned, I get the expected output on windows side, but I get this message on the os x side:
-bash: edu.oakland.lecture.Alfa: command not found
I know it has to be something stupid simple; what am I overlooking?
Thanks!
The classpath separator on UNIX-like systems (such as OS X) is ':', not ';'. Your command should just be
java -classpath bin edu.oakland.lecture.Alfa
If you actually did have several different components to your classpath, it would look like:
java -classpath bin:foo edu.oakland.lecture.Alfa

Categories