简体   繁体   中英

java local variable not used in eclipse

This is an excerpt from the code:

    if (resultText.equalsIgnoreCase("open browser"))
        {
            try{
            Process p;  //resultText="";
            p = Runtime.getRuntime().exec("cmd /c start chrome.exe");

            }catch(Exception ae){}
        }

In the eclipse its showing a warning as:

The value of local variable p is not used

What's wrong in here? Please help me understand the problem. Thank you.

This is just a warning from the IDE. It's not an error. It's just letting you know that p is never being used. You could just make your code into:

if (resultText.equalsIgnoreCase("open browser"))
{
    try{
        Runtime.getRuntime().exec("cmd /c start chrome.exe");
    }catch(Exception ae){}
}

And it will do the same thing.

Why do you need to p at all ?

Process p;  //resultText="";
p = Runtime.getRuntime().exec("cmd /c start chrome.exe");

You are not using it after assign value to it. If you use it the warning will go away.

You code should be:

    if (resultText.equalsIgnoreCase("open browser"))
    {
        try{
            Runtime.getRuntime().exec("cmd /c start chrome.exe");
        }catch(Exception ae){}
    }

As you don't need variable p at all.

The warning is telling you that you are assigning a value to p , but never using it afterwards.

It is a warning, so it is ignorable.

Basically its not actually an error. It's your Eclipse, warning you that you aren't actually reading that variable (p), so you aren't receiving any input from it.

Nowhere in the code, you use the variable p.

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

You can ignore warnings.

It's just telling you that you assigned a value to p but never used it.

Try this:

    if (resultText.equalsIgnoreCase("open browser"))
    {
        try{
            Runtime.getRuntime().exec("cmd /c start chrome.exe");

        }catch(Exception ae){
            System.out.println("An error has oucurd... :d sorry");
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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