LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   use gcc or javac for java compile? (https://www.linuxquestions.org/questions/programming-9/use-gcc-or-javac-for-java-compile-241695/)

jordanGSU 10-12-2004 09:09 AM

use gcc or javac for java compile?
 
Trying to see which is best.
1) gcc can be compiled with java support.
2) javac is the compiler that comes from SUN.

Which one do you use--and why? I have not tried either in Linux, but under windows I use javac.

Mega Man X 10-12-2004 09:28 AM

Gcj converts java byte code into machine language, so it will be faster then running inside a virtual machine. It's a great attempt of the called "Just in time compilers", a hype that appeared about the same time when Java started to be widely used outside web-applets...

gcj really is neat, but it's quite limited when it comes down to AWT and Swing classics (java's graphical Interfaces for buttons, frames, windows, etc).

At the end, it's all up to you: Do you need to use AWT or Swing for your applications and/or are following a book about Java? Or maybe portability? Because if you use gcj to convert Java byte code into machine language you can kiss you platform independent code good bye... If so is the case, then stick with Java2 from Sun, otherwise it's worthy to take a look into gcj which, unlike Sun's compiler, it's open source...

jordanGSU 10-12-2004 09:32 AM

Thanks, I have not heard that Gcj converts bytecode into machine language...that should definately speed it up.

Mega Man X 10-12-2004 09:35 AM

Cool, no problems :). I've found an interesting link for you:

http://www.linuxjournal.com/article.php?sid=4860

I believe that Slackware ships with both Sun's Java2 SDK and gcc/gcj, so you could try it right away and see the performance differences ;).

Good luck!

CroMagnon 10-12-2004 06:54 PM

gcj doesn't HAVE to compile machine code - you can use -C to generate .class files instead.

jordanGSU 10-12-2004 07:21 PM

kinda interesting...
 
I was trying out gcj...expecting to see an improvement in performance. What I actually found was that a .class file seems to execute faster than machine-code, or at least in the one case that I tested. I'm thinking that this is probably a fluke...has anyone had similar experiences?

Tests:
--machine code--
giro@slackbox:~/java$ ./test
done...execution took: 9523 milliseconds
giro@slackbox:~/java$ ./test
done...execution took: 9243 milliseconds
giro@slackbox:~/java$ ./test
done...execution took: 9486 milliseconds

--byte code--
giro@slackbox:~/java$ java test
done...execution took: 2974 milliseconds
giro@slackbox:~/java$ java test
done...execution took: 2942 milliseconds
giro@slackbox:~/java$ java test
done...execution took: 2940 milliseconds

Heres how I compiled:
--to compile to machine-code:
gcj -c -g -O test.java
gcj --main=test -o test test.o

--to compile to byte-code:
javac test.java


Source Code:
public class test
{
public static void main(String []args)
{
long start=System.currentTimeMillis();
double poo=1;
for(int i=0;i<1000000;i++)
for(int j=0;j<10;j++)
{ poo=Math.random();
poo=poo*2;
poo=Math.random();
poo=poo*2;
}
long stop=System.currentTimeMillis();
long result=stop-start;
System.out.println("done...execution took: "+result+" milliseconds");
}
}

Robhogg 10-12-2004 07:37 PM

I'm sticking with using javac at the moment, for two main reasons: It's the way described in the book I'm learning from, and I can use it on My Mac and in Windows as well as in Linux.

Incidentally, any opinions on introductory books? I feel that the one I started off with ("teach yourself Java" by Chris Wright) is a bit lacking in explanation. I think I would have been making more progress with a book that had told me, for instance, the implications of declaring a method to be static :rolleyes:. i'm considering trying the Java book in SAMS' "Teach Yourself... in 24 Hours" series, as I've been impressed with their other books that I've looked at.

CroMagnon 10-12-2004 08:22 PM

I think I'd blame gcj's optimization versus Hotspot's. The Hotspot compiler also compiles native versions of the Math.Random functions, while gcj's assembler output doesn't appear to contain it, so it's probably calling a less efficient version.
If you want to see the java version run much slower, add -Xint to the command line to force it to interpreted mode ;)

jlliagre 10-13-2004 02:46 AM

Its interesting to see how the biased Megaman X reply has been proved wrong by a real world experience.
Quote:

Gcj converts java byte code into machine language, so it will be faster then running inside a virtual machine. It's a great attempt of the called "Just in time compilers", a hype that appeared about the same time when Java started to be widely used outside web-applets...
Just in time compilers are not a hype but a reality, and have improved themselves enough for java to be competitive with other languages in term of performance. Gcj is not using a just in time compiler, but (without -C) just a plain compiler that has the disadvantage of loosing java classes portability.

WhiteChedda 10-13-2004 07:12 AM

Quote:

Originally posted by Robhogg
I'm sticking with using javac at the moment, for two main reasons: It's the way described in the book I'm learning from, and I can use it on My Mac and in Windows as well as in Linux.


GCC works those 3 platforms and more, just FYI.

http://gcc.gnu.org/install/specific.html

Mac OSX is built on BSD, which like linux is a unix variant. Pretty much anything that runs on a unix platform can be found for OSX.

Mega Man X 10-13-2004 07:45 AM

Quote:

Originally posted by jlliagre
Its interesting to see how the biased Megaman X reply has been proved wrong by a real world experience.

Just in time compilers are not a hype but a reality, and have improved themselves enough for java to be competitive with other languages in term of performance. Gcj is not using a just in time compiler, but (without -C) just a plain compiler that has the disadvantage of loosing java classes portability.

Don't you just love to when you try to help somebody, in an easy way to understand, as saying "Wine is basically an emulator", so some jack*** jumps in and tries to prove how wrong you were? :).

Please note that I said a hype that "were". Just in time compilers do, exist today but they did not when Java came out.

I also never said that it was not possible to compile Java to byte code with gcj, did I?

Not sure what is going on in this forum lately... looks like peoples always try to prove you wrong or reply to an already answered question to increase post numbers or something :rolleyes:

To make it clear, I never said you could not compile Java code to byte code (.class) with gcj, nor did I said that JITC _are_ a hype, but _were_...

If anybody had actually read the link I've posted, it's clearly quoted:

Quote:

Features and Limitations of GCJ

GCJ is not only a compiler. It is intended to be a complete Java environment with features similar to Sun's JDK. If you specify the -C option to gcj it will compile to standard .class files. Specifically, the goal is that gcj -C should be a plugin replacement for Sun's javac command.
so nobody has found the Ultimate Super Combo Finish here... you are all running over other peoples posts without reading it...

Regards!

jordanGSU 10-13-2004 08:20 AM

hey...no flame wars :)
Everyone's opinion is important to me, although I do like to hear conflicting ideas because they give me something to investigate...lets not bash anyone.
thanks

jlliagre 10-13-2004 12:56 PM

Hi Megaman X, sorry for the way my posting was perceived, it went beyond what I wanted to express.

I was just pointing at your definitive sentence:
Quote:

so it will be faster than running inside a virtual machine
which just prove wrong for that particular example, nothing personal.

Just my 2 cents of Sun Java biased opinion ;)

Mega Man X 10-13-2004 01:43 PM

No problems jlliagre!. I was to strong on you. Been having one of the worst weeks ever for reasons that I won't botter you with, but includes from racism to trains being canceled in the butt place I live in Sweden for some 16 years old kids who wants attention.

I apologize. Really sorry mate. I won't post anything more here until I'm back on the tracks. I'm very un-social lately. Oh my, I so have to move away from here...

Regards!

titanium_geek 10-13-2004 08:20 PM

Quote:

Originally posted by Robhogg
I'm sticking with using javac at the moment, for two main reasons: It's the way described in the book I'm learning from, and I can use it on My Mac and in Windows as well as in Linux.

Incidentally, any opinions on introductory books? I feel that the one I started off with ("teach yourself Java" by Chris Wright) is a bit lacking in explanation. I think I would have been making more progress with a book that had told me, for instance, the implications of declaring a method to be static :rolleyes:. i'm considering trying the Java book in SAMS' "Teach Yourself... in 24 Hours" series, as I've been impressed with their other books that I've looked at.

As a class, we are working through SAMS teach yourself Java in 21 days, one chapter a week. We went for this one instead of the in 24 hours one, because In 24 Hours really skims... just enough to get you started. I was impressed with thier HTML in 24 hours, but not with the Linux in 24 Hours. we are also using the certification book as well, it really helped me flesh out my knowlege of java, even though I failed the exam.

anyway, I have always stuck to javac, I like the portability thing.

titanium_geek


All times are GMT -5. The time now is 03:12 PM.