简体   繁体   English

java对`main'collect2的未定义引用:ld返回1退出状态

[英]java undefined reference to `main' collect2: ld returned 1 exit status

I have a problem compiling a java file. 我在编译java文件时遇到问题。 My command for compile: javac -g HelloWorld.java 我的编译命令:javac -g HelloWorld.java

I wrote a simple hello world program: 我编写了一个简单的hello world程序:

public class HelloWorld {

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

}

I'm using putty , and I'm connecting with ssh . 我正在使用putty ,并且正在与ssh连接。 I am getting this error: 我收到此错误:

/usr/lib64/gcc/x86_64-suse-linux/4.6/../../../../lib64/crt1.o: In function `_start':
/home/abuild/rpmbuild/BUILD/glibc-2.14.1/csu/../sysdeps/x86_64/elf/start.S:109: undefined reference to `main'
collect2: ld returned 1 exit status

As I see, you are using the gcc javac compiler, which doesn't work as the SUN/Oracle implementation. 如我所见,您正在使用gcc javac编译器,它不能作为SUN / Oracle实现。

I guess you need to add --main=HelloWorld to the command. 我想您需要在命令中添加--main=HelloWorld Since several classes may have a 'main' method, the linker needs to be told which one to use. 由于多个类可能具有“主”方法,因此需要告知链接器使用哪个。

Also, you might want to add -o , otherwise, you'll get a 'a.out' file, which is the default executable name for the GNU compilers (and any C compiler actually). 另外,您可能想添加-o,否则,您将获得一个'a.out'文件,这是GNU编译器(以及实际上所有C编译器)的默认可执行文件名称。

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM