10

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:

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

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

https://i.sstatic.net/KrpsT.png

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

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

https://i.sstatic.net/Ryllw.pngI

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

2
  • 1
    You need a method public static void main(String[] argv) (exactly like that). Commented Jun 16, 2012 at 11:17
  • 1) "value of local variable p is not used" is a compile time warning. 2) A Launch Error is a run-time error rather than a compile time error. 3) Someone who is "really new to java" should try easier things than using platform specific code to create a Process. There is this 6 page article that describes some of the requirements for using a Process correctly. It will, for example, require a Thread to consume the output streams of the Process. Commented Jun 16, 2012 at 11:33

7 Answers 7

13

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

Sign up to request clarification or add additional context in comments.

Comments

1

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
   }
}

Comments

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.

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.

Comments

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:

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)

Comments

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.

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
}

Comments

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.

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

6 Comments

What is with the blank lines? I might put one before the main declaration, but the others are relatively pointless.
Thank you, maybe you could tell how to run an .exe which is packaged into .jar? I want to run not windows default calculator, but the one I downloaded earlier. This one - i.imgur.com/NGnqL.png
@Andrew Thompson This is for convenience) Since when working with large amounts of code - I share the space logical chunks, which simplifies the reading of the code in the future.
"maybe you could tell how to run an .exe which is packaged into .jar?" 1) Run it on Windows (only Windows will run an EXE unless an emulator is installed) 2) Extract it to a temporary file. 3) Run the temporary file. -- BTW 1) Unless this calculator calculates the meaning of "Life, the universe & everything" - it will probably be quicker to code it in Swing and use ScriptEngine for the calculations. It will also be cross-platform (or have the possibility to be x-plat.) 2) You asked 2 questions in the original question, both of which have been answered. Now a 3rd question..
@TorchTT "logical chunks" Seems you & me have different ideas of 'logical'. (shrugs)
|
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.