How does Java standard libraries comunicate with native code? - java

Do they use something like Mono's PInvoke? Or is it more like internal calls registered before the runtime is started? Does java have a base library for handling native calls like mscorlib.dll? If I want to invoke a JVM in C code will it libraries look for the .so/.dll files? Does it make a difference to Java standard libraries if I statically link all of the JRE natives libraries?

They use JNI, exactly as it is publicly documented, to invoke native shared libraries for the specific platform.
As far as invoking a JVM from C code, the JVM uses shared libraries (DLL, SO, etc). A quick search of the JDK 6 source code does not reveal any System.loadLibrary() for the core native support (like native methods in Object, String, etc). That suggests to me that the native code for these methods, which appears to be in DLL's judging from the contents of the JRE/bin directory, is explicitly linked by java.exe (and javaw.exe in Windows).
When I last looked at this stuff, the requirements for invoking a JVM from C code was a well documented part of JNI - I strongly suggest you refer to that doco to proceed further. We even went so far as successfully writing a native C wrapper/loader for the IBM AS/400 Java 1.1 JVM.

They use the Java Native Interface (JNI).
I've never called the JVM from C, so I don't know about that.

There is an example here about how to start a JVM from inside your C program:
http://www.inonit.com/cygwin/jni/invocationApi/c.html

"Java Native Access (JNA) provides Java programs easy access to native shared libraries (DLLs on Windows) without writing anything but Java code" (quotation from their homepage).
Personally never tried it so far.

Related

What are native libraries and native methods interface in JVM?

I came across this figure while studying JVM. I understood all the components except "Native method interface" and "native method libraries". What are those exactly?
Native method interface: Native method interface is an interface that connects native method libraries (implemented in C, C++ etc.) with JVM for executing native methods.
Native method library: Implementation in native code.
Please refer this link for more information.
Native Method Interface (JNI) is part of JDK that connects Java code with native applications and libraries that are written in other programming languages, such as C, C++, and assembly.
Why use JNI?
Use features that are platform-dependent and are not supported in the Java class library.
Increase performance by implementing a time-critical code in a lower-level language.
Access libraries that already written in another programming language.
Native Method Libraries are libraries that are written in other programming languages, such as C, C++, and assembly.
These libraries can be loaded through JNI.
So, the picture you posted is saying that JNI allows access to Native Method Libraries.
Java Native Interface:
The above diagram you could come across when you are studying about java virtual machine functionality. There is nothing like Native library when you are installing and working with java at first time. Those are all added when we are developing our own library , but it should be in other languages.
When you are developing functionality in other languages Java Virtual machine will include those libraries at the execution level(Third level) of the java application.
To add to this, there are also native libraries in jvm $JAVA_HOME/jre/lib/amd64/ and these are core libraries which are loaded by reflection(null/boot classloader) which is why they are available at run-time while compiling and we're able to use native methods of Object class like getClass(). So, not only for custom development using JNI but also, some of the core functionalities of java are written into native.

Is it possible to use Java to create dll?

Want to create animation dll for Window XP
Is it ok to create Java2d animation and export as dll??
Yes. You need to write code in C++ to start the JVM with the invocation interface to JNI, and call into it. However, you may find it difficult to create windows in this way that integrate seamlessly with your Windows environment application to display your animation. This is a rather advanced JNI usage, and I'd recommend reading the JNI book before even trying a little bit of it.
I am pretty sure you can only create .Jar files from java not dlls
I doubt so, unless there's some 3rd party tools out there. For your case where graphics is involved, chances are even lower.
Actually, what Quentin said should work. When you compile java to native with GCJ you first compile the .java files into platform specific .o (object) files. Presumably you would compile the .o files into a dll rather than an exe. GCJ also includes components like the garbage collector, and base java libraries. None of which require a JVM to run. The downer is that the dll would be huge. A simple "Hello World" app when compiled with GCJ is ~35MB, thanks to all the default libs and the garbage collector. Likewise your dll would be huge.
There are "bridges" that allow Java and non-Java code to call into one another. Depending on what you are trying to accomplish, these might be useful as you could write your Java code and then call into it from a C++ or C# DLL, depending on which language you are creating your DLL with, which will also determine what kind of bridge you need. I have never seen a freely provided bridge though. All the ones I've found when looking had to be purchased.
No, IIRC you can't. DLLs are linked directly when loaded. Java code needs a jvm, so you can only provide a dll that starts a jvm and starts code there, but not all necessarily stuff fits in the dll.
You should not do this. It looks like you're trying to use the wrong approach for your problem.
Well…
GCJ is available for Windows.
GCJ is part of GCC.
GCC can create dlls.
It might be possible to put that together to build DLLs using GCJ.
Yes, it is possible to generate DLLs from Java source code.
2 Methods I have used:
IKVM
Graal
IKVM is mature, but rather slow in runtime execution of the generated DLL.
Graal is fast, but early days and immature in the Windows environment.
See https://openjdk.java.net/jeps/295 for further info.
There are other commercial options available as well.
I agree with bmargulies. It's probably feasible for an expert, but it would be a large DLL and you'd be mixing technologies that were never made to work together. It doesn't make sense to try this, in my opinion.

JNI vs Runtime.exec()

I have two options - I can either use JNI or use Runtime.exec to execute a C++ library.
The C++ program is CPU intensive and long running i.e. some calls may take up to a couple of hours to return.
What are the pros and cons of each?
Which approach should I go for?
If you need to interact with the C++ library, go for JNI.
If it's a standalone program that you just want to invoke (and perhaps grab the output), Runtime.exec() is much, much simpler.
Have you looked into JNA as a third option?
From the JNA site:
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 guide for an introduction.
Using JNI may restrict your ability to move from 32bit to 64 bit. You may also find you have to tune the application memory settings as well.
Unless you know how well the C++ stuff is written - it could make your app more unstable. You are lucky in that the C++ bit takes a few hours - so just call it externally.

How to load third party libraries (like X11 libs) through code in Java?

Whenever JVM fires up, i.e when java command is run; it looks for other libraries in /java/jre/lib folder. These libraries along with the third party libraries like X11 libs, are loaded into the memory by the system's dynamic loader (like dld.so in HP Unix).
So is it possible to load third party libraries from code in java? If yes, what could be the side effects?
What you are looking for is the Java Native Interface (JNI). Using external native libraries will make your code less portable. It will make your code less stable since none of the Java language guarantees will hold. There can be security manager implications since you application will need permissions to load the library. In my experience there are many difficulties in writing good JNI code; specifically deallocation and debugging.
SWIG may be of use automatically generate the necessary JNI code.
Native libraries are loaded with System/Runtime.loadLibrary. You will then need some code to make translate between the libraries native calls and JNI (Java Native Interface). Then you will need some Java code with native methods defined for calling into the translation code.

Can you use Java libraries in a VB.net program?

I'm wondering if a Java library can be called from a VB.net application.
(A Google search turns up lots of shady answers, but nothing definitive)
No, you can't. Unless you are willing to use some "J#" libraries (which is not nearly the same as Java) or IKVM which is a Java implementation that runs on top of .NET, but as their documentation says:
IKVM.OpenJDK.ClassLibrary.dll: compiled version of the Java class libraries derived from the OpenJDK class library with some parts filled in with code from GNU Classpath and IcedTea, plus some additional IKVM.NET specific code.
So it's not the real deal.
I am author of jni4net, open source intraprocess bridge between JVM and CLR. It's build on top of JNI and PInvoke. No C/C++ code needed. I hope it will help you.
You can call Java from .NET if you wrap it in some form to make it accessable and the easiest way is typically to use a Runtime bridge like
http://www.jnbridge.com/
Other way is to wrap your API with java webservices.
check this also http://www.devx.com/interop/Article/19945
Nothing out of the box.
Most java/.net interop that I know uses web services.
If you can create COM components with Java, you can use tlbimp to create an interop assembly for using in VB.Net.
If can create standard DLLs that can be used from C++ with Java, you can write P/Invoke declarations and call them from VB.Net.
If you can create a web service with Java, you can generate proxy class from the WSDL and call it from VB.Net.
In any case, chances are the Java component will live in a separate process. I doubt you can load both the Java VM and the CLR in the same process.
If you have the source code and compile it using the J# compiler, then the answer is yes. If you want to call any pre-Java 2 (aka 1.2) libraries, then these are included pretty much verbatim with J#. More recent stuff is going to be tricky though (i.e., it's not there).
An example where this is used commercially are the yFiles graph layout algorithms from yWorks. These were originally just a Java library, but for the past few years they've been offering a .NET version, which is just the Java version compiled with Visual J#.
It's not without problems, and there are some limitations that you can't get around, but it can be done. So... unfortunately this answer looks pretty shady as well.
You could use JNI to instantiate a virtual machine and then use Java Classes. It will be some fun, though, because you would need to use C++ as a bridge between VB.Net and Java.
This article in java world has a quick tutorial on how to use Java from C++ and viceversa.
http://www.javaworld.com/javaworld/javatips/jw-javatip17.html
If you have the source, Visual Studio will let you convert Java code into c#.

Categories

Resources