Get VM Arguments using JNI - java

I'd like to know if it is possible to get the VM arguments using JNI?
Using the Invocation API allows you to specify the VM arguments if you are creating your own JVM.
What I'd like to be able to do is query these arguments in JNI from an already running JVM that has been lauched using the normal Java launcher. I believe that it must be possible because JMX is able to do so.
I've searched quite extensivley for this and as yet have not found a solution.
Thanks in advance
CND
PS. I know it is possible to query these using RuntimeMXBean.getInputArguments() but I need to do this natively in JNI.

RuntimeMXBean.getInputArguments() calls through to VMMangementImpl.getVmArguments which returns an immutable collections, making it difficult to tamper with.
http://www.java2s.com/Open-Source/Java-Document/6.0-JDK-Modules-sun/management/sun/management/VMManagementImpl.java.htm
It in turn calls a native method getVmArguments0() and the source for this method is available in the OpenJDK. You might be able to call whatever this method calls.

Related

Does the JVM use system calls to seek OS functionalities?

We know that the JVM calls on the underlying system to allocate memory and CPU time, access files, and many more. How does it work internally to achieve its activities?
Does the JVM use system calls?
Does the JVM use system calls?
Yes.
How does it work internally to achieve its activities?
The typical pattern is that some of the methods in a Java class are labelled as native. When the JVM encounters a call to a native method it makes a call into C or C++ code that is part of the JVM executable. The native method implementation typically does the following:
Check arguments from Java, and translates them into a C / C++ compatible form. For example, String arguments need to be converted to zero-terminated form.
Call the standard C / C++ library function with the arguments it needs.
The library function makes the syscall.
The OS does its stuff and the syscall returns.
The standard C / C++ library function returns.
The native method implementation checks the 'errno'. If there was an error, it creates a Java exception object and throws it.
Otherwise, the native method implementation converts results, etc into Java objects and returns them to the caller of the Java method.
The details vary, depending on what the native method does.
If you want to get a deeper understanding, I recommend that you checkout a copy of the OpenJDK source tree and start trawling. (You need to do the hard yards yourself ....)
Indeed, JVM needs to leverage system calls which is an operating system way to allow processes to interact with underlying system resources.
You can run strace java -version to see a bunch of system calls (mmap, mprotect, openat, etc.) executed even during this very limited java/jvm run.
Another good way to find out more is to dig trough JVM sources for native methods.
One example could be an implementation of FileChannel#force method
which internally calls fsync system call (for example): https://github.com/AdoptOpenJDK/openjdk-jdk11u/blob/5f01925b80ed851b133ee26fbcb07026ac04149e/src/java.base/unix/native/libnio/ch/FileDispatcherImpl.c#L172
Yes, system calls are the only way that an OS allows access to any program.
In the case of Java, this is why some OS-specific “features” show through, so spoiling the ideal of write-once-run-anywhere. For example, I’ve had a program that I developed on a Windows box fail when run on a Linux box.
The problem turned out to be that in the resources directory, the filename was all-lower case, but my program had the file name inMixed case. The program worked on windows since filenames on windows are case-insensitive, but in Linux they are case-sensitive.

How to use .Net dll in Java

I have dll created in vb.net.
How can i use its functions in JAVA.
I found something JNI while searching on google , but not getting it.
Is there any simple documentation with example.
I would recommend Java Native Access (JNA) as its easier than using JNI. Lets say you have a DLL with some functions,
Create an java interface which has the same method signatures as the functions in DLL.
For example
public interface NativeExample{
public int method1(String param1);
public boolean mehthod2();
}
Now following is the way you load the DLL (assuming its name is NativeLib.dll)
NativeExample nativeExample= (NativeExample) Native.loadLibrary("NativeLib",
NativeExample.class);
Once you have this, you can call the method from the DLL via java methods.
`nativeExample.method("test");`
`nativeExample.method2();`
For mappings of the datatypes between Java and Native, please refer the the link above.
Here is one more example.
Exactly JNI will not give you direct access to .NET unless you work a lot on this. You can build own wrappers and use C/C++ as middle-ware but for small projects it will never pay off. Remember that calling method is one thing but passing arguments in proper types, retrieving results, subscribing events etc..etc.. is a lot more.
Therefore I would also propose to check for third-party tools which are well prepared for these kind of scenarios.
First check at least these two:
Javonet
JNBridge
Javonet I would recommend for all small and quick projects as this is light solution which provide very high performance due to in process communication and does all background work for you. All you need is call "AddReference(your.dll)" and next invoke any method using reflection-style API. You can invoke any methods, set/get fields and properties, get results, subscribe events or handle exceptions.
Very similar way works JNBridge which has a lit bit more additional staff/extensions for popular enterprise scenarios like cloud integration or websphere but this one I would recommend for bigger projects were you expect to bridge java and .net on separate machines it's more powerful but more complicated and heavy as well.
Both are free to try and Javonet is free for non-commercial and academic usage, so try, test and choose what best suits your requirements.
Well, there are third party tools / libraries that will help you connect Java to .NET. If you want to do it yourself -- meaning implement the JNI wrappers yourself -- you actually need to implement 2 sets of wrappers.
JNI will get you to C/C++, which is not allowed to directly access .NET objects. At that point, you can implement another wrapper, a .NET object with only static methods, that your C/C++ wrapper can call. Since unmanaged C/C++ code can't own .NET objects, it can only call static methods of .NET classes.

Declare function from JNI

Is there any way to create native function from jni without creating dll? I mean like in python http://docs.python.org/2/extending/embedding.html
Section 5.4. Extending Embedded Python
I don't want to use dll exported functions.
Regards
You can embbed VM in a native application, call into Java from C/C++ then callback from Java back into C/C++. See the Invocation API in JNI documentation. This way there is no need for dynamic linking (DLLs). You can also dynamically generate classes in runtime by generating bytecode with native methods (e.g. with ASM) and then registering whatever C/C++ function pointers you need with RegisterNatives.
technically it is possible.
Around 6 or 8 years ago I saw a C++ implementation (it was at codeproject site presented), which created a JVM and did access Java classes. Isn't very popular, for very good reasons, are to many to enumerate here, but is possible.
I would strongly recommend to do the other side, exactly what you don't want: java invoke the dll or so, but to many reasons, but is up to you...
Perhaps JNA does what you want?

Using System.load vs library set as agent

I made a simple library for measuring the size of a given object. The library is
accessed through JNI from a Java class. It's specifically designed for Hotspot, thus it uses the JVMTI and it calls GetObjectSize.
My question is, what's the better solution?
To load the library using System.load("library") or
to set the library as an agent and explicitly load it by issuing the java command with the agent option.
All similar examples I've seen so far employ the agent function:
Determine Java class size from JNI jclass
How to calbulate the size of an object
In what way is that more efficient than just loading the library?
Thanks!
The advantage of command-line injection, is that the agent is loaded together with the Virtual Machine. You can inject the agent into every Java program without modifying the program. With System.load that this done at the point of invocation. You can react to a bigger set of callbacks, for instance there is a VM Initialization Event. If you use System.load that event is bygone.
As a result, I do not believe that using command-line injection is so much more efficient in a cetain way, but rather more effective for some use cases.

In one process, how can we create another process that is an exact copy of the parent?

Using java in one process, how can we create another process that is an exact copy of the parent?
You can't do that using pure Java. In the C world you'd simply use the fork() system call, but Java has no direct support for this.
You might be able to call fork() from some JNI code (or using JNA), but I doubt that your average JVM implementation is built to handle that gracefully.
You can create another process which has the same command line arguments with Runtime.exec(). However you cannot do a C-style fork from pure Java. It would be heard to imagine why you would want to.
I suspect what you are trying to do is best done another way. Can you give more details as you why you want to do this?

Categories