简体   繁体   English

在编译我的简单Java文件时出现编译错误

[英]Compile error during compilation of my simple Java file

I have written a simple java program mentioned in below. 我写了下面提到的一个简单的Java程序。 Unfortunately a compile error occurs. 不幸的是,发生编译错误。

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

The following comes out during compilation at command prompt: 在编译过程中,在命令提示符下显示以下内容:

c:\\Java>java String Error: Main method not found in class String, please define the main method as: public static void main(String[] args) c:\\ Java> java String错误:在类String中找不到主要方法,请将其定义为:public static void main(String [] args)

It wasn't working for any of my programs, not even this simple one! 它不适用于我的任何程序,甚至不是这个简单的程序! Why is this? 为什么是这样?

EDIT: 编辑:

Now I have: 我现在有:

import java.lang.*;
import java.lang.String.*;

class incogn {
    public static void main(String[] args) {
        System.out.println("stre");
    }
}

And its not working. 并且它不起作用。 Why isn't that working? 为什么这样不起作用?

It says the exact thing as before. 它说的和以前一样。

What you said with the Java.lang.String[] works, but why won't this? 您对Java.lang.String []所说的有效,但是为什么不这样呢? And why haven't I needed to put any of this on before? 为什么我以前不需要放任何东西呢?

It's probably because you are using the name String for your class, colliding with java.lang.String ; 可能是因为您在类中使用名称String ,与java.lang.String冲突; in other words, you have written that your main method takes instances of your class as input, rather than the one that the main method needs to take, namely java.lang.String . 换句话说,您已经写到您的main方法将类的实例作为输入,而不是main方法需要的一个实例(即java.lang.String Try either 尝试之一

  1. Renaming your class 重命名课程
  2. Changing your signature to public static void main(java.lang.String[] args) { 将您的签名更改为public static void main(java.lang.String[] args) {

Well, giving your class name String and then call main(String[] args) is ambiguous, if it means java.lang.String or your newly defined class, but with java scope rules, it will use your, unless otherwise declared. 好吧,给您的类名称为String然后调用main(String[] args)是模棱两可的,如果这意味着java.lang.String或您新定义的类,但是使用Java作用域规则,除非另有声明,否则它将使用您的类。


Update after edit 编辑后更新

There's no java.lang.String package , but it is a class . 没有java.lang.String ,但这是一个 Therefore, you can't say 因此,你不能说

import java.lang.String.*;

By adding the " .* " the compiler understands that this refers to some package String . 通过添加“ .* ”,编译器将理解这是指某些程序包String

One exception, is the static import, but this is not what you need here. 一个例外是静态导入,但这不是您所需要的。

You should not call your class String, it will conflict with the java class String. 您不应调用您的类String,它将与Java类String冲突。

Or, you can change your code to: 或者,您可以将代码更改为:

class String {
    public static void main(java.lang.String[] args) {
         System.out.println("stre");
    }
}
  1. Don't create a java file named "String" as it is the java library class in java.lang.String 不要创建名为“ String”的Java文件,因为它是java.lang.String中的Java库类。
  2. Make the class as public 公开授课

So, write your source code as below: 因此,编写您的源代码如下:

public class StringDemo {
    public static void main(String[] args) {
         System.out.println("stre");
    }
}

Compile it now. 现在编译。 I believe it would work well. 我相信这会很好。

1) The class should be public 1)上课应该是public

2) Avoid to call you class String , as it has the duplicate name with java.lang.String (You still can, but not good). 2)避免调用String类,因为它与java.lang.String具有重复的名称(您仍然可以,但是不好)。 Otherwise, your main method is having wrong type of arguments. 否则,您的主要方法的参数类型错误。

3) You don't have to explicitly import from java.lang package. 3)您不必从java.lang包中显式导入。


public class String {
    public static void main(java.lang.String[] args) {
         System.out.println("stre");
    }
}

OR 要么

public class YourClass {
    public static void main(String[] args) {
         System.out.println("stre");
    }
}

Try to use the second form. 尝试使用第二种形式。

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

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