-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?
User
  • 3
  • 3
  • 2
    Please read [What is the X Y Problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-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.](https://www.spigotmc.org/threads/compiled-native-spigot-binary.56869/) 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
  • @rwong That is actually an interesting idea to go about it, but, preferably, I wouldn't want them to have to use a different Spigot. – User May 29 '18 at 19:27
  • As written this question requires one to guess what you are trying to do; instead add some clarity what are you trying to do? why do you think this will solve your problem? Also its not clear that this is a software engineering question and not a help me debug question. – esoterik May 29 '18 at 20:10
  • @esoterik I'm trying to further understand exactly what I asked. I do not have a current implementation of this as I am trying to understand the theory behind my idea to see if it will work. I believe that my post is clear; if something is not, you're going to have to tell me what is unclear. – User May 29 '18 at 20:20
  • 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
  • @esoterik I thought it was self-evident from the questions. I am trying to convert to native code to help deter tampering and reverse engineering. Notice I said "help." I don't need to be told it's impossible. I am simply asking if it is possible to convert to native code and have it work and interact the same way if it's run by a Java server. And then also asking questions about the technicalities of it. – User May 29 '18 at 20:48
  • 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
  • The question can be rephrased as "what is the equivalent for excelsior jet, except that it compiles each public method in each public class in a JAR, instead of just compiling code that is called from the main method". The *non-specific* response to this is that (1) questions asking for tools and resources are out-of-topic (2) that compilation approach sounds like too inefficient (there is overhead calling from Java into native; and then if the native code needs to access more Java objects, it will have to freeze (lock) the JRE and/or ask JRE to marshal data back and forth) etc. – rwong May 30 '18 at 00:27
  • @esoterik Converting code to native will do *nothing* to prevent tampering or reverse-engineering. – Sean Burton May 30 '18 at 13:46
  • @Sean Burton Definitely not with someone skilled enough, but it does make it much harder for your average Java developer (I was thinking of putting other systems in place to help). Additionally, the main problem I face is just that the classes end up loaded in the JVM. – User May 30 '18 at 14:50

2 Answers2

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
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.

esoterik
  • 3,879
  • 3
  • 13
  • 22