Question

Is it, at least theoretically, possible to convert a Java application into native code that can be run by something else written in Java? One example of this could be a Minecraft Spigot server. You can code plugins and stick them in the plugins folder of the Spigot server. The server then runs and then runs the plugins which interact with the server. Would it be possible to compile it to native code? If so, how (Spigot plugins don't have a main method, which is why excelsior jet won't convert it; is there a way around that?)? And would the plugin interact normally with the Java server, considering that the Java server would have to interact with native code?

One additional question is since the native code ends up running in the JVM, is it any more secure than a normal Java application running in memory? What I mean is when you run a Java application, it's in memory and you can dump the bytecode. Would the native code be any different than if it weren't native?

Am I thinking about any of this wrong? I ask that you also include any references or articles in your answers.

Thanks

EDIT: When I'm talking about converting Java to native code, I am talking about using something like Excelsior Jet, explained here, which is essentially an AOT compiler which also has a JIT compiler for anything that needs to be run on the fly.

My specific questions are as follows:

  • Can I convert something that needs to be run by something else written in Java?
  • Will the converted plugin interact the same way with the server than if it weren't native code?
  • Is the native code then run in the JVM? Or is it exclusively interacted with by the JVM?
  • Can the bytecode then be dumped the same way that normal bytecode can be dumped? (example here or by modifying the OpenJDK)
  • And, finally, would the native code be as easily reverse engineered or as readable as normal Java code?
Was it helpful?

Solution

Am I thinking about any of this wrong?

Yes! Very much yes. But this is an assumption, since i don't really know what you are trying to do; the true answer is probably.

Can I convert something that needs to be run by something else written in Java?

How is this 'something else' running the code? If it expects Java bytecode, and you give it native it will not work. Does 'something else' shell invoke the target? Can you modify something else? If so you could, in theory at least, convert the entire thing. This is what rwong meant by providing the entry point.

Will the converted plugin interact the same way with the server than if it weren't native code?

No! Not without changes to expect native code and a way to execute them. (Assuming conversion is possible.)

Is the native code then run in the JVM? Or is it exclusively interacted with by the JVM?

The JVM is written in native code and it runs java bytecode not native; its not going to run native code.

Can the bytecode then be dumped the same way that normal bytecode can be dumped?

What bytecode, you are talking about converting to native code; probably.

And, finally, would the native code be as easily reverse engineered or as readable as normal Java code?

Java source code or java byte code? Either way this is largely irrelevant.

OTHER TIPS

Regarding your first question, it is totally possible to build a plugin and have it load at runtime. In C#, I have done this with success using Microsoft's Managed Extensibility Framework. It seems that Java has alternatives to do the same thing though I have not attempted it.

I am not aware of any possiblity to use these types of frameworks with native code. In general, unless the code is already written in a native language, there would be no reason to move it from Java to a native language, especially given the host program is written in Java.

However, the application obviously has to be aware and watching for these plugins for them to be loaded. That means you would have to alter your main application to use one of these frameworks or somehow implement it yourself.

Licensed under: CC-BY-SA with attribution
scroll top