1

I´d wanted to compile a simple Java "Hello World" program like it was repesented on the GeeksforGeeks Hello World Tutorial, by using gcj in Linux Ubuntu. This is the source code:

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

But gcj threw two errors:

  1. (.text+0x18): undefined reference to main
  2. collect2: error: ld returned 1 exit status

Original output from the terminal:

gcj -o helloworld HelloWorld.java
/usr/lib/gcc/i686-linux-gnu/4.6/../../../i386-linux-gnu/crt1.o: In function `_start':
(.text+0x18): undefined reference to `main'
collect2: error: ld returned 1 exit status

I´d take attention on the requirement, that the .java file should be named after the class which holds main:

Important Points :

  • The name of the class defined by the program is HelloWorld, which is same as name of file(HelloWorld.java). This is not a coincidence. In Java, all codes must reside inside a class and there is at most one public class which contain main() method.
  • By convention, the name of the main class(class which contain main method) should match the name of the file that holds the program.

What am I doing wrong?

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110

1 Answers1

1

You are missing the --main= option, from the documentation, this option is used when linking to specify the name of the class whose main method should be invoked when the resulting executable is run.

gcj -o helloworld --main=HelloWorld HelloWorld.java
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • 1
    @RobertS You may want to look at [GraalVM](https://www.graalvm.org/), GCJ (from [Wikipedia](https://en.wikipedia.org/wiki/GNU_Compiler_for_Java)) *As of 2015, there were no new developments announced from GCJ and the product was in maintenance mode. GCJ was removed from the GCC trunk on September 30, 2016. Announcement of its removal was made with the release of the GCC 7.1, which does not contain it. GCJ remains part of GCC 6.* – Elliott Frisch Jan 18 '20 at 17:41
  • Thank you for your kind information and the useful links. Yes, I have encountered that the development of GCJ has been suspended. I think, I will try to find another Java compiler for Linux, also for the compatibility with newer Java code. – RobertS supports Monica Cellio Jan 18 '20 at 17:49
  • 1
    Graal (and native-image) are the more recent alternatives with which I am familiar. – Elliott Frisch Jan 18 '20 at 18:14