Recompiling a dynamic Java application to a native Windows executable

463 views Asked by At

Would it be possible to "emulate" the dynamic code generation that is done via Spring AOP, Dependency Injection and other Spring mechanisms when we recompile a java application to a native one? How about Hibernate and pure java reflection? Some of the tools that are capable of translating Java to native code include the GCC and the Excelsior compilers, but do they provide reliable native code that will work in the same way as does the Java application?

The whole story

Currently, we are facing a weird scenario at work. We have a server-side java web application that uses Spring with its dependency injection, messaging, MVC and AOP. The application is quite a large one, with complex structure and dependencies, involving lots of third parties and has a huge code base.

The problem is that we have to target a Windows system (Windows 7 embedded; Core2 Duo; 4GB RAM) that should host the above application. We have certain limitations (hardware, software and legal) which disallow us to use pure java code on the target hardware. The hardware operates a Windows operating system, and the only allowed option for us is a native executable (EXE).

So, we have to convert (a soft word for it) the java server application (or a minimal subset of it) to native one for the device. According to certain resources on the internet, this might be possible trough certain compilers that produce native (non-JAVA) binaries. However, due to severe difference between a dynamic platform (as Java is) and the static nature of the native one, some of the JVM features will not be available - for instance dynamic compilation and proxies (according to this page). So it seems a nearly impossible task for an application that is relying on dynamic fundamental technologies like Spring's DI, AOP and transaction management. Not to mention that we are using Hibernate as an ORM.

I must say that the whole application will be trimmed as much as possible and only the desired functionality will be left (as it is quite standalone - in terms of not relying on third party services to work), but still we cannot say goodbye to core concepts like Spring, Hibernate and AOP. Therefore, my question is: is it somehow possible successfully convert such an application to a native code (having the above properties and effective limitations)? Would it be possible to "emulate" the dynamic code generation that is done via the AOP (like writing our proxies as static instead if relying on the AOP) and other Spring mechanisms? Some of the tools that are mentioned to do this include the GCC and the Excelsior compilers. Do they provide reliable result with the dynamic technologies I mentioned? Is there any alternative besides rewriting the application on a more-static language like C/C++ (which is unacceptable for our time frame)? I'd also appreciate any arguments in support for not doing so, as we are currently just evaluating the opportunity. Any well-argumented rejection is acceptable for me, as myself personally have never done such native code conversion and am not aware of the full impacts of it over the software.

2

There are 2 answers

1
lscoughlin On BEST ANSWER

Your only real target option are things like IKVM and JNBridgePro -- however, i'm guessing a .net application is not really reasonable either given the way you described your constraints, and depending on the technical details of the application IKVM

In that case, you're pretty much asking for the impossible. There is no sane strategy for compiling java down to win32 native code that supports any significant subset of the average enterprise application tool chain.

It will be cheaper to re-write the application from scratch for the target platform then it will be to create the infrastructure and do the testing required to hack/port it from it's target runtime to a wholly unrelated foreign runtime.

UPDATE: Some things have changed: There is now graalvm which will generate native images for your java code. It's free and fancy and does largely what OP wanted.

1
m_eric On

This is a very interesting problem: How does the compiler determine dynamically loaded classes up front?

I took a look at the Excelsior documentation and it seems to handle this by Mixed Compilation Model. The JET runtime involves a JIT compiler that kicks in for dynamic scenarios. I think converting a dynamic Java program into native one is a really hard problem and not feasible to solve in your particular scenario.

Nevertheless, .NET Team recently showcased a new compile time technology that does what you asked for Microsoft managed apps (i.e for .NET not Java). Currently, it is only supported for Windows Store apps so it is not useful for your scenario. However, there is a blog you may want to check out: http://blogs.msdn.com/b/dotnet/archive/2014/05/20/net-native-deep-dive-dynamic-features-in-static-code.asp to get insight about their solution to this interesting problem.

They discuss the mechanism they introduced to support dynamic scenarios in a static compilation model. In a nutshell, the native .NET compiler does its best to predict all the classes that will be used by the app. For classes that can't be predicted statically, it requires "hints" to be supplied to the compiler. App developers and library developers specify runTime directives to enable dynamic scenarios for specified types. Runtime directives allow adding Reflection metadata or serialization support for specified types. In this model, there is no JIT, so you would run into a runtime exception if your type was not inferred by the compiler or not included in Runtime Directives xml file.