You are not logged in.

#1 2006-04-06 16:03:50

riwa
Member
From: Lund, Sweden
Registered: 2006-04-04
Posts: 215

Compiling Java?

I'm really new to programming, have done quite a bit in python, a few simple c programs and now I'm trying to learn Java (think it's a better start than c). However my program returns an error. To me it looks kinda strange (but I don't know how java works) but it looks like the piece of code is just a class definition. There's no calling it. But I get an error compiling it:

015312/home/riwa/# gcj hello.java
hello.java:1: error: Public class 'WesternTown' must be defined in a file called 'WesternTown.java'.
   public class WesternTown
                ^
1 error

I'm using the gnu compiler..


If you must have must. Have must.
- DKE supporters about this wonderful swedish soda.

Offline

#2 2006-04-06 16:18:41

Chman
Member
Registered: 2006-01-31
Posts: 169
Website

Re: Compiling Java?

If your main class is WesternTown, the file must be named WesternTown.java as said in the error message... Rename hello.java to WesternTown.java.

That's all wink

Offline

#3 2006-04-06 16:26:33

riwa
Member
From: Lund, Sweden
Registered: 2006-04-04
Posts: 215

Re: Compiling Java?

022448/home/riwa# gcj Hello.java
/usr/lib/gcc/i686-pc-linux-gnu/4.0.3/../../../crt1.o: In function `_start':
: undefined reference to `main'
collect2: ld returned 1 exit status
022458/home/riwa#

I wrote a "hello world" program to try but it didn't work.

class Hello {
    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

Now what's wrong?


If you must have must. Have must.
- DKE supporters about this wonderful swedish soda.

Offline

#4 2006-04-06 16:42:12

Chman
Member
Registered: 2006-01-31
Posts: 169
Website

Re: Compiling Java?

Well, the code is correct but I never used gcj, maybe there's something wrong with your classpath or gcj...

Offline

#5 2006-04-06 19:13:57

fedaykin
Member
Registered: 2004-12-02
Posts: 25

Re: Compiling Java?

Yeah, the code is fine, it works with javac. 

I tried it with gcj and got the same thing, but I found this: http://gcc.gnu.org/java/faq.html#4_1

and it works fine if I do  "gcj --main=Hello Hello.java".

Offline

#6 2006-04-14 11:16:12

user
Member
Registered: 2006-03-29
Posts: 465

Re: Compiling Java?

For the beginner, gcj isn't good choice.
install java6 , ant 1.6.5 binary, eclipse3.2

be happy!


I removed my sig, cause i select the flag, the flag often the target of enemy.

SAR brain-tumor
[img]http://img91.imageshack.us/img91/460/cellphonethumb0ff.jpg[/img]

Offline

#7 2006-04-15 06:29:23

rohandhruva
Member
Registered: 2005-04-23
Posts: 25

Re: Compiling Java?

Tell ya what ? Your gcj syntax is wrong. Use javac, from the j2sdk package. Also, there was this cool program in the knoppix disc that allowed gcj to be called with javac syntax -- copy paste the below code, and put it in /usr/local/bin as file name "javac-gcj" .. Then you can have the absolute fast speed of gcj vs. the clunky slow javac wink

#!/usr/bin/perl -w
#
# Starts the GNU Java compiler.
#
# Command-line arguments should be in the style of Sun's Java compiler;
# these will be converted to gcj arguments before being passed to the
# gcj itself.
#
# Copyright (C) 2002-2003 by Ben Burton <bab@debian.org>
# Based on the original gcj-wrapper-3.2 shell script.

use strict;

# The real Java compiler:
my $javaCompiler = '/usr/bin/gcj';

# The command-line arguments to pass to the real Java compiler:
my @commandLine;

# The warning flags to pass to the GNU Java compiler:
my $warnings = '-Wall';

# Build the command-line from the arguments given.
my $parsingOptions = 1;
my $copyNextArg = 0;
my $ignoreNextArg = 0;
my $appendNextArg = '';
foreach my $arg (@ARGV) {
    # See if we already know what to do with this argument.
    if ($ignoreNextArg) {
        # Throw it away.
        $ignoreNextArg = 0;
        next;
    } elsif ($copyNextArg or not $parsingOptions) {
        # Copy it directly.
        push @commandLine, $arg;
        $copyNextArg = 0;
        next;
    } elsif ($appendNextArg) {
        # Append it to $appendNextArg and then copy directly.
        push @commandLine, ($appendNextArg . $arg);
        $appendNextArg = '';
        next;
    }

    # Try to interpret Sun-style options.
    if ($arg eq '-version') {
        push @commandLine, '--version';
    } elsif ($arg eq '-h' or $arg eq '-help') {
        push @commandLine, '--help';
    } elsif ($arg eq '-classpath' or $arg eq '--classpath' or $arg eq '--cp') {
        $appendNextArg = '--classpath=';
    } elsif ($arg eq '-encoding' or $arg eq '-bootclasspath' or
             $arg eq '-extdirs') {
        $appendNextArg = '-' . $arg . '=';
    } elsif ($arg eq '-d') {
        push @commandLine, '-d';
        $copyNextArg = 1;
    } elsif ($arg eq '-nowarn') {
        $warnings = '';
    } elsif ($arg =~ /^-g/) {
        # Some kind of debugging option - just switch debugging on.
        push @commandLine, '-g' if ($arg ne '-g:none');
    } elsif ($arg eq '-O') {
        push @commandLine, '-O2';
    } elsif ($arg =~ /^-X/) {
        # An extended Sun option (which we don't support).
        push @commandLine, '--help' if ($arg eq '-X');
    } elsif ($arg eq '-source' or $arg eq '-sourcepath' or $arg eq '-target') {
        # An unsupported option with a following argument.
        $ignoreNextArg = 1;
    } elsif ($arg =~ /^-/) {
        # An unsupported standalone option.
    } else {
        # Some non-option argument has been given.
        # Stop parsing options at this point.
        push @commandLine, $arg;
        $parsingOptions = 0;
    }
}

# Was there a partial argument that was never completed?
push @commandLine, $appendNextArg if ($appendNextArg);

# Call the real Java compiler.
my @fullCommandLine = ( $javaCompiler, '-C' );
push @fullCommandLine, $warnings if ($warnings);
push @fullCommandLine, @commandLine;
exec @fullCommandLine or exit(1);

HTH.

Offline

Board footer

Powered by FluxBB