Christophe De Troyer
Christophe De Troyer

Reputation: 2922

Can't run rar.exe through Process

I'm trying to unpack a rar file from my Java application.

I've found plenty of solutions on how to execute a command which works for stuff like dir etc on Windows. But it doesn't seem to work for any other application like gcc or rar. I know they both work because when I execute them in cmd they give me proper output.

However, I'd like to print the output they spit out in the cmd. I just don't really get why it works for dir and for nothing else (none "native" commands).

The exitVal always seems to be 0..

    try {
        Runtime rt = Runtime.getRuntime();

        Process pr = rt.exec("cmd /c rar.exe"); //Does not work

        //Process pr = rt.exec("cmd /c dir") // This works
        //Process pr = rt.exec("gcc.exe");

        BufferedReader input = new BufferedReader(new InputStreamReader(pr.getInputStream()));

        String line=null;

        while((line=input.readLine()) != null) {
            System.out.println(line);
        }

        int exitVal = pr.waitFor();
        System.out.println("Exited with error code "+exitVal);
    }catch(IOException e)
    {
        System.out.println("An error occured. Are you sure the executable is in your Windows PATH?");
    }
    catch(Exception e) {
        System.out.println(e.me);
        e.printStackTrace();
    }
     catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
    }

Upvotes: 0

Views: 437

Answers (2)

samnaction
samnaction

Reputation: 1254

You cant straight away run rar.exe on cmd. It will give
'rar.exe' is not recognized as an internal or external command, operable program or batch file.

You either specify the path or add the path of Rar in environment variable

Upvotes: 0

A Paul
A Paul

Reputation: 8261

Can you try something like below

Process p = Runtime.getRuntime().exec(new String[]{"c:\\some_path\\UnRAR.exe", "e","c:\\some_another_path\\Archive.rar"}); 

or you can use below library

https://github.com/edmund-wagner/junrar

Upvotes: 1

Related Questions