Java error “Value of local variable is not used”

后端 未结 7 1302
温柔的废话
温柔的废话 2020-12-06 18:00

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

相关标签:
7条回答
  • 2020-12-06 18:31

    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.

    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

    0 讨论(0)
  • 2020-12-06 18:34

    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.

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

    rt.exec("calc.exe")
    

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

    0 讨论(0)
  • 2020-12-06 18:43

    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.

    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
    }
    
    0 讨论(0)
  • 2020-12-06 18:43

    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.

    To remove the error - it is necessary to remove the variable "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

    0 讨论(0)
  • 2020-12-06 18:49

    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:

    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).

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

    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)

    0 讨论(0)
  • 2020-12-06 18:55

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

    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
       }
    }
    
    0 讨论(0)
提交回复
热议问题