英文:
Unknown command error when using multithread in notepad compiled by cmd
问题
class Multi extends Thread{
public void run (){
System.out.println("thread is running");
}
}
class Test3 {
public static void main (String[] args){
Multi n = new Multi();
n.start();
for(int i = 0; i<20; i++){
try{
Thread.sleep(1000);
}catch(Exception e){
}
System.out.println("thread is running..." + Thread.currentThread().getName());
}
}
}
英文:
I am new at Java. I am facing a problem when I was trying to run a multi-threading code in cmd. I am using notepad for writing.
Error:
Thread.java:1: error: duplicate class: Multi
class Multi extends Thread{
^
Thread.java:6: error: duplicate class: Test3
class Test3 {
^
Thread.java:1: error: cannot access Thread
class Multi extends Thread{
^
bad source file: .\Thread.java
file does not contain class Thread
Please remove or make sure it appears in the correct subdirectory of the sourcepath.
Thread.java:10: error: cannot find symbol
n.start();
^
symbol: method start()
location: variable n of type Multi
4 errors
class Multi extends Thread{
public void run (){
System.out.println("thread is running");
}
}
class Test3 {
public static void main (String[] args){
Multi n = new Multi();
n.start();
for(int i = 0; i<20; i++){
try{
Thread.sleep(1000);
}catch(Exception e){
}
System.out.println("thread is running..."+Thread.currentThread().getName());
}
}
}
答案1
得分: 0
在Java中,文件名必须对应于其中的类名。如果你将类命名为'Thread.java',这实际上是一个不好的想法,你需要将类命名为Thread。
尝试将文件重命名为'Multi.java'。
英文:
In Java it is neccessary that the filename corresponds to the name of the class in it. If you name your class 'Thread.java' which is actually a bad idea you need to name the class Thread.
Try renaming your file to 'Multi.java'.
答案2
得分: 0
以下是翻译好的内容:
所有的 Java 代码都包含在类中。一个包含 Java 源代码的单个文件必须包含恰好一个公共类的定义。在您的情况下,这意味着文件 Thread.java
必须包含以下内容:
public class Thread {
// 类的定义
}
该文件可以包含其他类定义,但它们不能是公共类。因此,文件 Thread.java
可以包含类 Multi
的定义。代码可能如下所示:
public class Thread {
}
class Multi {
}
另一个选项是使用嵌套类。
请注意,您不应该将您的类命名为 Thread
,因为那是 JDK 中的一个类,使用相同名称的两个类可能会引起混淆。
从您问题中的代码来看,我建议创建一个名为 Multi.java
的文件,其中将包含 [public] 类 Multi
的代码。
public class Multi extends Thread {
public void run() {
System.out.println("线程正在运行");
}
}
class Test3 {
public static void main(String[] args) {
Multi n = new Multi();
n.start();
for (int i = 0; i < 20; i++) {
try {
Thread.sleep(1000);
} catch (Exception e) {
}
System.out.println("线程正在运行..." + Thread.currentThread().getName());
}
}
}
英文:
All java code is contained in classes. A single file containing java source code must contain the definition of exactly one public class. In your case, this means that file Thread.java
must contain the following:
public class Thread {
// definition of class
}
The file can contain other class definitions but they cannot be public classes. Hence file Thread.java
can contain the definition for class Multi
. The code could be as follows.
public class Thread {
}
class Multi {
}
Another option is to use nested classes.
Note that you should not name your class Thread
since that is one of the classes in the JDK and having two classes with the same name may cause confusion.
Looking at the code in your question, I suggest creating a file named Multi.java
that will contain the code for [public] class Multi
.
public class Multi extends Thread {
public void run (){
System.out.println("thread is running");
}
}
class Test3 {
public static void main (String[] args){
Multi n = new Multi();
n.start();
for(int i = 0; i<20; i++){
try{
Thread.sleep(1000);
}catch(Exception e){
}
System.out.println("thread is running..."+Thread.currentThread().getName());
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论