How to compile .jar Java byte-codes into native machine codes?
Linux - SoftwareThis forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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?
Distribution: Linux Mint 9 x64, Linux From Scratch
Posts: 46
Rep:
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.
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):
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.
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.
Distribution: Linux Mint 9 x64, Linux From Scratch
Posts: 46
Rep:
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...
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.
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?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.