managing un-managed codes in JAVA? - java

like in C#,
Is it possible to manage un-managed in JAVA?
I want to use system dll like kernel32.dll, user32.dll or gdi32.dll.....

Java can call native code via the Java Native Interface (JNI). Java Native Access (JNA) is a wrapper around JNI that makes it more convenient to use.

Sure. Have a look at JNI.
Some useful links to get you started:
Wikipedia: Java Native Interface
Java Native Interface: Programmer's Guide and Specification
jGuru: JNI FAQ Home Page

Yes, no problem. Have a look at JNI, the java native interface.

Related

Does LevelDB support java?

I want to know if LevelDB supports java ?
Where can i get the LevelDB. There are no files under http://code.google.com/p/leveldb/
You can use the https://github.com/fusesource/leveldbjni java library which gives you a Java API to LevelDB via JNI.
There is also a Pure Java implementation of LevelDB available at https://github.com/dain/leveldb
Both the JNI wrapped and pure Java implementation implement the same interface classes so it's really easy to switch between the two.
LevelDB currently does not ship with JNI bindings, but you can wrap your own JNI binding around the file db/c.h via SWIG and the like.
You can have a try of leveldb-java. This is a pure Java version of LevelDB.
From looking at the source, i can say that it doesn't support java.
For the source use svn checkout http://leveldb.googlecode.com/svn/trunk/ leveldb-read-only

How to use a C API in Java? JNI?

I'm building a J2EE project, in which I would like to use an API which is only available in C.
I was thinking of using JNI to do so, but after a quick look at this tutorial, it looks that I in order to use JNI, I need to have the source code (.c files) to compile some kind of "JNI library".
In my case, the API only comports the .h with the signature of all the methods, and the already compiled .dll (or .so).
How could I do this?
Thank you!
JNA is a JNI-based library that allows calling normal C functions without needing a JNI-specific wrapper for each one.
Check out JNA. It allows you to use the .DLL directly. All you need to do is write a Java interface with the same functions you need from the .DLL.
Create a small C wrapper for the native library, compile this to a .dll/.so.
For each needed function in the existing C api, create one JNI-compliant C function which simply calls the real API.
JNI offers a pretty low level API for interfacing your Java code with native code. If you are OK with shelling out money, Jinvoke looks like a pretty good alternative which doesn't require you to write any C/C++ code. The plus here is that you get full paid support. If you don't require it, you can go with JNA. Anything but JNI IMO...

Calling Java library from Objective C on Mac

I want to create native Mac OS X application using Cocoa + Objective C but I need to connect to proprietary data source, and for this, owner of the data source only provides Java library. So I need to somehow import this Java library into my project and call functions on its Java classes.(Or create java wrapper around this library and then call my wrapper from objective-C).
Now, how can I do this? Quick google search leads me to JNI but I haven't found any good and actual(current) article/tutorial. I would really need some HOW TO article, how to load this java library, start VM if needed, and how to create java objects and call functions on them. Really something simple and I can move from there. Thanks.
Just to clarify, I repeat: I WANT to call Java functions from Objective-C, I do NOT want to call native functions from Java.
You're probably looking for the Invocation API, a little-known corner of Java Native Interface (JNI) which allows you to load the Java runtime in-process.
That said, you might have an easier time of it with a Java service application that communicates with your Objective-C application over network sockets.
You're looking for the Java-Objective C bridge, try looking at this article or on Apple's developer site. Be aware it is deprecated, that is it isn't being kept up to date with changes to Cocoa. But if you're just using it for an API passing standard Java datatypes you should be OK.

Is there a Java library to access the native Windows API?

Is there a Java library to access the native Windows API? Either with COM or JNI.
You could try these two, I have seen success with both.
http://jawinproject.sourceforge.net
The Java/Win32 integration project
(Jawin) is a free, open source
architecture for interoperation
between Java and components exposed
through Microsoft's Component Object
Model (COM) or through Win32 Dynamic
Link Libraries (DLLs).
https://github.com/twall/jna/
JNA provides Java programs easy access
to native shared libraries (DLLs on
Windows) without writing anything but
Java code—no JNI or native code is
required. This functionality is
comparable to Windows' Platform/Invoke
and Python's ctypes. Access is dynamic
at runtime without code generation.
JNA allows you to call directly into
native functions using natural Java
method invocation. The Java call looks
just like it does in native code. Most
calls require no special handling or
configuration; no boilerplate or
generated code is required.
Also read up here:
http://en.wikipedia.org/wiki/Java_Native_Interface
The Java Native Interface (JNI) is a
programming framework that allows Java
code running in a Java Virtual Machine
(JVM) to call and to be called1 by
native applications (programs specific
to a hardware and operating system
platform) and libraries written in
other languages, such as C, C++ and
assembly.
http://en.wikipedia.org/wiki/Java_Native_Access
Java Native Access provides Java
programs easy access to native shared
libraries without using the Java
Native Interface. JNA's design aims to
provide native access in a natural way
with a minimum of effort. No
boilerplate or generated glue code is
required.
JNA is pretty nice. I'm just a beginner and I found it very easy. Works not only for the Win32 API but for almost any other DLL.
Jacob is quite good on the COM side (but it's real COM - like you write in C++ - if you are familiar with true COM programming, then Jacob is a snap to use)
JNIWrapper or ConfyJ from from TeamDev.
One more option is WinRun4J. It has a native binding layer that aims to be compatible with pinvoke.net (the native binding format used in dot net). See examples for more information. Its a little early days so YMMV.
(full disclosure: i work on the project).
Yet another option is JFFI - this is used in jruby and jython to interact with native libraries.
Check out Waffle

Do you know of a Java library to access the native linux api?

Do you know of a Java library to access the native linux api?
I guess something like this must use JNI. So be it.
Have a look at JNA.
Here is the summary of JNA from their home page.
JNA provides Java programs easy access to native shared libraries (DLLs on Windows) without writing anything but Java code—no JNI or native code is required. This functionality is comparable to Windows' Platform/Invoke and Python's ctypes. Access is dynamic at runtime without code generation.
See their getting started page for some samples. Like calling native printf.
jtux might help depending on what you want to do.
You can use: Runtime.exec to execute anything you want...
SWIG makes life easier than "raw" JNI with javah etc.

Categories