Eclipse 如何创建一个编译问题未解决的类? [英] How can Eclipse create a class with unresolved compilation problems?

查看:20
本文介绍了Eclipse 如何创建一个编译问题未解决的类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试用 javac 编译这个类时,我得到一个编译错误并且没有创建 Test.class.

公共类测试{公共静态无效主(字符串 [] args){整数 x = 1L;//<- 这无法编译}}

但是当我在 Eclipse 中创建这个类时,我可以看到 Test.class 出现在 target/classes 中.当我尝试使用 java.exe 从命令行运行这个类时,我得到 <块引用>

线程main"中的异常java.lang.Error:未解决的编译问题:
类型不匹配:无法从 long 转换为 int

Eclipse 是否使用自己特殊的 Java 编译器来创建损坏的 .class?java.exe 如何知道.class 中的编译问题?

解决方案

这是 Java 编译器如何知道类中的编译错误.

public static void main(String[] paramArrayOfString){throw new Error("未解决的编译问题:
	类型不匹配:无法从long转换为int.
");}

如果你反编译你的class文件,你可以看到上面编译器生成的class文件的main()方法.这是因为 Eclipse 使用了 compiler(Eclipse Compiler forJava) 与标准的 Java 编译器不同!

When I try to compile this class with javac, I get a compilation error and Test.class is not created.

public class Test {
    public static void main(String[] args) {
        int x = 1L;  // <- this cannot compile
    }
}

But when I create this class in Eclipse, I can see that Test.class appears in target/classes. When I try to run this class from command line with java.exe, I get

Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Type mismatch: cannot convert from long to int

Does Eclipse use its own special Java compiler to create a broken .class? How does java.exe know about complilation problems in .class?

解决方案

This is how the Java compiler knows about the compilation error in the class.

public static void main(String[] paramArrayOfString)
{
    throw new Error("Unresolved compilation problem: 
	Type mismatch: cannot convert from long to int.
");
}

If you decompile your class file, you can see the above main() method of the class file, which the compiler has generated. This is because of the compiler which Eclipse uses (Eclipse Compiler for Java) is not the same as the standard Java compiler!

这篇关于Eclipse 如何创建一个编译问题未解决的类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆