java undefined reference `main'收集2 : ld returned 1 exit status

标签 java linux

我在编译 java 文件时遇到问题。 我的编译命令:javac -g HelloWorld.java

我编写了一个简单的 hello world 程序:

public class HelloWorld {

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

}

我正在使用 putty,并使用 ssh 连接。我收到此错误:

/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

最佳答案

正如我所见,您正在使用 gcc javac 编译器,它不能作为 SUN/Oracle 实现工作。

我想您需要将 --main=HelloWorld 添加到命令中。由于多个类可能有一个“main”方法,因此需要告诉链接器使用哪一个。

此外,您可能需要添加 -o ,否则,您将得到一个“a.out”文件,这是 GNU 编译器(实际上是任何 C 编译器)的默认可执行文件名称。

希望这有帮助!

关于java undefined reference `main'收集2 : ld returned 1 exit status,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19979650/

相关文章:

java - Spring security - 启用 HTTPS - 客户端收到 403 未经授权

linux - Linux 中的绝对路径(文件路径中有空格)

linux - Docker 容器中 GUI 应用的 X11 转发

android - 无法使用错误 : There's another emulator instance running with the current AVD 启动 android 模拟器

java - 如何捕获 Spring MVC 中的整数溢出错误?

java - Spring Rest Data 如何更改默认格式从 json 响应中删除 "_"下划线

java - 如何根据特定顺序对 hibernate 结果进行排序

java - org.hibernate.MappingException : property mapping has wrong number of columns in ENUM entity

c - Fd[0] 之后的 dup2!= STDIN_FILENO

linux - Linux 上的 Silverlight