LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   How to compile .jar Java byte-codes into native machine codes? (https://www.linuxquestions.org/questions/linux-software-2/how-to-compile-jar-java-byte-codes-into-native-machine-codes-829064/)

ilgaar 08-28-2010 06:46 PM

How to compile .jar Java byte-codes into native machine codes?
 
Hello, I am using Linux and I come across applications that are written in Java and are compiled to .jar executable Java byte-codes, however this applications require a Java runtime environment which consumes resources. I was thinking if there is a convenient way to compile these codes into native machine binary codes, to make a fast, compact application without the need for a Java environment. I know I can compile Java codes with gcj into binary codes, however in order to compile these applications one needs the .java files, which I have no access to. Is there a way to compile an executable .jar application into a native machine code in Linux? I'm not familiar with java, so I would be glad if you explain the process in details. What do I need?

Blender3D 08-28-2010 06:57 PM

Java is made to run from a .jar file. That is what is (kind of) simple about Java: one .jar file runs on 32-bit and 64-bit computers, even if they have Windows, Mac, Or Linux. It is really compatible.

Quick Google search gave me this: http://gcc.gnu.org/java/

As for the source code, the .jar is actually a zip file. Extract it, and you should find a .class file somewhere.
I only know of DJ Java Decompiler for Windows, but you should be able to decompile the .class file into a .java file.

That might get you a bit farther.

To compile it, get GCJ (this is for Ubuntu):
Code:

sudo apt-get install gcj
Then, compile it (I am guessing that this will work. If not, post what happens):
Code:

gcj /path/to/your.file -o /path/to/your/output.file
Now run the binary and see what happens (I'm not sure if this includes all of the libraries and classes, so it might not work at all).

Good luck.

ilgaar 08-28-2010 08:12 PM

Thanks for the reply. I upacked the jar file...
 
Thanks for the detailed reply. You are right .jar file is an archive, so I unpacked it. now I have two .class files, one is Launcher.class and the other is Launcher$1.class, I'm not sure what they mean exactly, and which one is the correct one. I also have JD Java Decompiler that I just downloaded and installed, which is very powerful, I can see the source code without even decompressing the .jar file. hmm.. there are too many files. There should be a practical way to this. If I can run .jar file with a Java runtime environment, then there also should be a way to compile it into binary native machine code.

ilgaar 08-28-2010 08:40 PM

I decompiled the whole package
 
I decompiled the whole package, and now I have a folder with hundreds of source files, I don't think decompiling is the right path. But because there was nothing to lose I tried to compile the Launcher.java which was a Launcher.class previously into a native machine code with a usual

Quote:

gcj --main=Launcher -o Launcher Launcher.java
and just as I expected, I got numerous warnings and errors. as an example :

Quote:

Javadev/install.source/org/jvnet/lafplugin/PluginManager.java:83: warning: Set is a raw type. References to generic type Set<E> should be parameterized
/* */ public Set getAvailablePlugins()
I don't think decompiling is the right path or even practical. Though I have never wrote a single line code in Java, the decompiler was a really powerful application.
By the way, I tried DJ and Jad java decomipler, and unfortunately for some reason they are not very Linux friendly, and neither worked for me.

Blender3D 08-28-2010 09:28 PM

Hmm, just what the doctor expected ;)

Me, being an amateur Java programmer (I can read the code at the very least), I am thinking that this might be some error with the changes in the new versions of Java, as I remember something of the form "Array<type>" or something like that mentioned in one of my books as a new feature. Maybe the compiler is a bit too old...

Why exactly are you trying to compile it into native machine code? I don't think that there will be any speed changes, if that's what you're hoping for...

Would you mind posting the application (a link would be good) you are trying to compile? I could take a stab at it, if you would like...

jay73 08-28-2010 10:26 PM

Using raw types shouldn't be a problem. Developers are urged to transition to typed generics but it the advice not absolutely mandatory. I think the real problem you'll experience is that gcj is many versions behind the sun (not Oracle) JDK or the open JDK, especially the graphics part. I think gcj hardly supports, say, swing at all. You would have to code a native GUI (using something like gnome-java) for a properly functioning binary.

ilgaar 08-28-2010 11:20 PM

So far it has been great
 
Alright. thanks for all your information. As far as I can understand from what you are telling me, gcj is way behind to support SUN Java or even Open JDK. That's a good piece of info.
now that I have the sources, (thanks to my powerful decompiler).
The only problem is to compile them into native machine code.
What do you recommend, other than gcj of course.
Any open source application to accomplish this task? any idea?


All times are GMT -5. The time now is 12:37 AM.