Java错误“不使用局部变量的值”

时间:2022-11-05 19:36:52

I am really new to java (started learning 2 days ago). Sorry if this is a stupid question. I am trying to learn how to use rt.exec & similar methods so I tried to make a very simple program which runs calc.exe. This is the code:

我对java很陌生(两天前开始学习)。对不起,如果这是一个愚蠢的问题。我正在尝试学习如何使用rt.exec和类似的方法,所以我尝试制作一个非常简单的程序,运行calc.exe。这是代码:

public class main {
{
try {
Runtime rt = Runtime.getRuntime() ;
Process p = rt.exec("calc.exe") ;
 }

catch(Exception exc){/*handle exception*/}
    }
}

Java错误“不使用局部变量的值”

I get the error " The value of local variable p is not used".

我得到的错误是“本地变量p的值没有被使用”。

And if I try to compile this is what I get:

如果我试着编译这就是我得到的:

Java错误“不使用局部变量的值”

I think it's easy to fix but I don't know how. Would be nice if someone helped.

我认为这很容易修复,但我不知道怎么修复。如果有人帮忙就好了。

7 个解决方案

#1


11  

Well, the error "The value of local variable p is not used.", Is not actually an error. It's your IDE (Eclipse), warning you that you aren't actually reading that variable, so you aren't receiving any input from it.

不使用局部变量p的值,实际上并不是一个错误。它是您的IDE (Eclipse),警告您实际上没有读取该变量,因此您没有从它接收任何输入。

And the other problem with your class is, you don't have a main method. Like this,

你的类的另一个问题是,你没有一个主方法。像这样,

public class main {
public static void main(String[] args) {
try {
Runtime rt = Runtime.getRuntime() ;
Process p = rt.exec("calc.exe") ;
} catch(Exception exc){
/*handle exception*/
}
    }
}

And by the way, you should always start a class name with a captial letter. So public class main, should actually be public class Main

顺便说一下,你应该以一个大写字母开始一个类名。所以公共阶级的主体,其实应该是公共阶级的主体。

#2


1  

You get that error because you don't have the main method that is used to start the java program:

你会得到那个错误,因为你没有用来启动java程序的主要方法:

public class main {

public static void main(String[] args) {
   try {
       Runtime rt = Runtime.getRuntime() ;
       Process p = rt.exec("calc.exe") ; // here, eclipse is WARINING(so you can ignore it) you that that the variable p is never used(it's just a warning)
   } catch(Exception exc) {
       /*handle exception*/
       // never do this, always put at least a System.out.println("some error here" + e); so you don't ignore a potential exception
   }
}

#3


1  

I believe what you have is not an error but a warning; eclipse (and other IDEs/compilers) will tell you that, although you assigned a value to the variable p, you did not use it anywhere. It tells you this because this is sometimes an error; mostly when you assign a value to a variable, you later use that variable in some way.

我相信你所拥有的不是一个错误,而是一个警告;eclipse(以及其他ide /编译器)将告诉您,尽管您为变量p分配了一个值,但是您在任何地方都没有使用它。它告诉你这个,因为有时候这是一个错误;通常,当你给一个变量赋值时,你以后会以某种方式使用这个变量。

You can eliminate the error by changing that particular statement to just

你可以通过将特定的语句改为just来消除错误

rt.exec("calc.exe")

since you are not required to assign a value from the call to exec.

因为不需要从调用exec中分配值。

#4


0  

There is no such thing as a stupid qiestion(only misplaced ones, in the worst case).

根本就没有愚蠢的咀嚼(在最坏的情况下,只是放错了地方)。

The "Editor does not contain a main type" refers to the fact that you have not defined a main method. All java programs require a main method, as such:

“编辑器不包含主类型”指的是您没有定义主方法。所有java程序都需要一个主方法,比如:

public static void main(String [] args){
    <code>
}

This is where you must place your code.

这是您必须放置代码的地方。

The "Value not used" is just a warning; it tells you that your variable p only exists within the try-block. You can declare your p-variable before the try - that way, you can use it outside the try-scope(the scope of a variable refers to where it exists, in this case, only inside the try-block).

“未使用的值”只是一个警告;它告诉您变量p只存在于try-block中。您可以在try之前声明您的p-variable——这样,您可以在try-scope之外使用它(在本例中,变量的作用域指的是它存在的地方,在try-block中)。

If you want to use your p, this is what you're after:

如果你想用你的p,这就是你想要的:

public class Main {
    public static void main(String[] args) {
        Process p;
        try {
            Runtime rt = Runtime.getRuntime();
            p = rt.exec("calc.exe");
        } catch(Exception exc) {/*handle exception*/}
    }
}

[EDIT]: Note that it is part of the java coding convention to use Capital letters for the first letter of a class, e.g. Main.java(not main.java)

(编辑):注意它是java编码惯例的一部分,使用大写字母作为一个类的第一个字母,例如Main。java(不是main.java)

#5


0  

The use of the variable is not in issue here. That error appears because JVM needs a method with the signature to know where to start execution.

变量的使用在这里不是问题。出现这个错误是因为JVM需要一个带有签名的方法来知道从哪里开始执行。

public static void main( String args[] ){ //TODO: Stuff here } 

Introduce a method with this signature in your class, and it shall clear that error. Alternatively, you may embed your code in a static block as below - but this method is not to be recommended.

在类中引入一个带有此签名的方法,它将清除该错误。或者,您可以将您的代码嵌入到静态块中,如下所示——但是这种方法是不推荐的。

static {
    // TODO: Your code here
}

#6


0  

Error "The value of local variable p is not used" due to the fact that nowhere in the code, you do not use the variable p.

错误“不使用局部变量p的值”,因为代码中没有使用变量p。

To remove the error - it is necessary to remove the variable "p".

要删除错误——需要删除变量“p”。

To run the calculator, you must use the code:

要运行计算器,必须使用以下代码:

public class MainClass {

    public static void main(String args[]) throws IOException {

        Runtime.getRuntime().exec("cmd /c calc.exe");

    }

}

This and all other comments translated by Google Translate

谷歌翻译的所有评论

#7


0  

you are not using main how can you compile please use main method. Ans second one is use p local variable in your method.other wise declare p variable starting of method.

你不是用主怎么编译的,请用主方法。第二种方法是在方法中使用p局部变量。另一种方法是声明p变量。

#1


11  

Well, the error "The value of local variable p is not used.", Is not actually an error. It's your IDE (Eclipse), warning you that you aren't actually reading that variable, so you aren't receiving any input from it.

不使用局部变量p的值,实际上并不是一个错误。它是您的IDE (Eclipse),警告您实际上没有读取该变量,因此您没有从它接收任何输入。

And the other problem with your class is, you don't have a main method. Like this,

你的类的另一个问题是,你没有一个主方法。像这样,

public class main {
public static void main(String[] args) {
try {
Runtime rt = Runtime.getRuntime() ;
Process p = rt.exec("calc.exe") ;
} catch(Exception exc){
/*handle exception*/
}
    }
}

And by the way, you should always start a class name with a captial letter. So public class main, should actually be public class Main

顺便说一下,你应该以一个大写字母开始一个类名。所以公共阶级的主体,其实应该是公共阶级的主体。

#2


1  

You get that error because you don't have the main method that is used to start the java program:

你会得到那个错误,因为你没有用来启动java程序的主要方法:

public class main {

public static void main(String[] args) {
   try {
       Runtime rt = Runtime.getRuntime() ;
       Process p = rt.exec("calc.exe") ; // here, eclipse is WARINING(so you can ignore it) you that that the variable p is never used(it's just a warning)
   } catch(Exception exc) {
       /*handle exception*/
       // never do this, always put at least a System.out.println("some error here" + e); so you don't ignore a potential exception
   }
}

#3


1  

I believe what you have is not an error but a warning; eclipse (and other IDEs/compilers) will tell you that, although you assigned a value to the variable p, you did not use it anywhere. It tells you this because this is sometimes an error; mostly when you assign a value to a variable, you later use that variable in some way.

我相信你所拥有的不是一个错误,而是一个警告;eclipse(以及其他ide /编译器)将告诉您,尽管您为变量p分配了一个值,但是您在任何地方都没有使用它。它告诉你这个,因为有时候这是一个错误;通常,当你给一个变量赋值时,你以后会以某种方式使用这个变量。

You can eliminate the error by changing that particular statement to just

你可以通过将特定的语句改为just来消除错误

rt.exec("calc.exe")

since you are not required to assign a value from the call to exec.

因为不需要从调用exec中分配值。

#4


0  

There is no such thing as a stupid qiestion(only misplaced ones, in the worst case).

根本就没有愚蠢的咀嚼(在最坏的情况下,只是放错了地方)。

The "Editor does not contain a main type" refers to the fact that you have not defined a main method. All java programs require a main method, as such:

“编辑器不包含主类型”指的是您没有定义主方法。所有java程序都需要一个主方法,比如:

public static void main(String [] args){
    <code>
}

This is where you must place your code.

这是您必须放置代码的地方。

The "Value not used" is just a warning; it tells you that your variable p only exists within the try-block. You can declare your p-variable before the try - that way, you can use it outside the try-scope(the scope of a variable refers to where it exists, in this case, only inside the try-block).

“未使用的值”只是一个警告;它告诉您变量p只存在于try-block中。您可以在try之前声明您的p-variable——这样,您可以在try-scope之外使用它(在本例中,变量的作用域指的是它存在的地方,在try-block中)。

If you want to use your p, this is what you're after:

如果你想用你的p,这就是你想要的:

public class Main {
    public static void main(String[] args) {
        Process p;
        try {
            Runtime rt = Runtime.getRuntime();
            p = rt.exec("calc.exe");
        } catch(Exception exc) {/*handle exception*/}
    }
}

[EDIT]: Note that it is part of the java coding convention to use Capital letters for the first letter of a class, e.g. Main.java(not main.java)

(编辑):注意它是java编码惯例的一部分,使用大写字母作为一个类的第一个字母,例如Main。java(不是main.java)

#5


0  

The use of the variable is not in issue here. That error appears because JVM needs a method with the signature to know where to start execution.

变量的使用在这里不是问题。出现这个错误是因为JVM需要一个带有签名的方法来知道从哪里开始执行。

public static void main( String args[] ){ //TODO: Stuff here } 

Introduce a method with this signature in your class, and it shall clear that error. Alternatively, you may embed your code in a static block as below - but this method is not to be recommended.

在类中引入一个带有此签名的方法,它将清除该错误。或者,您可以将您的代码嵌入到静态块中,如下所示——但是这种方法是不推荐的。

static {
    // TODO: Your code here
}

#6


0  

Error "The value of local variable p is not used" due to the fact that nowhere in the code, you do not use the variable p.

错误“不使用局部变量p的值”,因为代码中没有使用变量p。

To remove the error - it is necessary to remove the variable "p".

要删除错误——需要删除变量“p”。

To run the calculator, you must use the code:

要运行计算器,必须使用以下代码:

public class MainClass {

    public static void main(String args[]) throws IOException {

        Runtime.getRuntime().exec("cmd /c calc.exe");

    }

}

This and all other comments translated by Google Translate

谷歌翻译的所有评论

#7


0  

you are not using main how can you compile please use main method. Ans second one is use p local variable in your method.other wise declare p variable starting of method.

你不是用主怎么编译的,请用主方法。第二种方法是在方法中使用p局部变量。另一种方法是声明p变量。