-2

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?
  • 2
    Please read What is the X Y Problem and respond accordingly. – Robert Harvey May 29 '18 at 18:41
  • Is this basically a question on the "entry point" ? The entry point for Spigot is inside the Spigot.jar, i.e. the server, not the plugin. Therefore, just point excelsior jet to it. Instructions. All of the plugins need to be in place, as compilation is triggered when the plugin is loaded. If the question is about performance, it is really difficult to answer without someone having performed the experiment first. – rwong May 29 '18 at 19:13
  • (Warning about previous link. DON'T download untrusted/unverified executables from anywhere. Learn about techniques and instructions, but don't download or run untrusted executables. You should have a basic understanding of information security, trojan horses, viruses, etc., and their basic prevention techniques. Virus scanners are not meant to catch all instances of malicious code.) – rwong May 29 '18 at 19:16
  • 3
    @WampyCakes what are you trying to do? what is your real problem? why are you trying to compile Java into native code. It's possible someone could have an answer for your real problem, but can't give it because you haven't asked. You have started with a solution but didn't give the problem. – esoterik May 29 '18 at 20:34
  • 1
    @WampyCakes It was not self evident, we are not mind-readers. You should edit your question to indicate that your goal is to slow/deter reverse engineering efforts. Then ask if your 'idea' will work. – esoterik May 29 '18 at 20:56
0

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.

0

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.

  • MEF is based entirely on managed code, not native code. – Robert Harvey May 29 '18 at 18:54
  • @RobertHarvey I can't argue that. But, the op asks about converting "Java code to native code to be run by Java". I guess maybe he means re-writing his Java plugin in another language and THEN loading it into his main Java application. I did not consider that at first. – Tyler Nichols May 29 '18 at 18:56
  • I honestly have no idea what he's talking about. Native code of any kind seems incompatible with the Java ecosystem. There are ways to run C++ on an Android phone alongside Java for reasons of performance, but that's platform-specific. – Robert Harvey May 29 '18 at 18:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.