Using winRAR with Java (help)

Using winRAR with java. I’m trying to use winrar through command line.

If I run this code the window’s title is “rar a compressed text.txt” (read “start”'s help file and it said that if a command is not provided, but a string is then the string will be the title of the window). I put winRAR in my PATH so I can run rar.exe from anywhere in cmd, but It’s not seeing it as a command, just a string.

Runtime.getRuntime().exec(new String[] {"cmd.exe", "/c", "start", "rar a compressed text.txt"});

also tried

Runtime.getRuntime().exec(new String[] {"cmd.exe", "/c", "start", "rar", "a compressed text.txt"});

which will run rar.exe (blazes through the parameters) but then disappears without RARing text.txt -> compressed.rar

Any thoughts?

  1. does it work on the command-line?
  2. do you grab and read the STDERR/STDOUT from the process, so they will don’t suffer from full buffers, blocking WinRAR ?
  1. Works in command line

  2. Not to sure what you are asking, so probably no

Here’s the code:

import java.io.File;
import java.io.IOException;

public class rarAllFolders
{
	
	public static void main(String args[])
	{
		File directory = new File(System.getProperty("user.dir"));
		File folders[] = directory.listFiles();

		for(int i = 0; i < folders.length; i++)
		{
			if(folders[i].getName().indexOf(".") == -1)
			{
				//rar all the folders
				try {
					System.out.println(folders[i].getName()+"                  " + folders[i]);
					Runtime.getRuntime().exec(new String[] {"cmd.exe", "/c", "start", "rar", "a compressed text.txt"}); //" + folders[i].getName() + " " + folders[i].getName()});
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		
	}
	
}

This program when run will rar all the folders in the current directory.

Runtime.exec() returns a java.lang.Process object. You can use that to read the error and output streams to see what is going on.

Thank You

I’m having trouble with buffered readers right now (probably because it’s late), but so far it just returns “-1” I don’t think i’m doing it right.
Been looking at

[url]http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html?page=4[/url]

Will play with it more in the morning, thank you ;D

Now the code looks like

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Reader;

public class rarAllFolders
{
	
	public static void main(String args[])
	{
		File directory = new File(System.getProperty("user.dir"));
		File folders[] = directory.listFiles();

		for(int i = 0; i < folders.length; i++)
		{
			if(folders[i].getName().indexOf(".") == -1)
			{
				//rar all the folders
				try {
					System.out.println(folders[i].getName()+"                  " + folders[i]);
					Process pros = Runtime.getRuntime().exec(new String[] {"cmd.exe", "/c", "start", "rar a compressed text.txt"}); //" + folders[i].getName() + " " + folders[i].getName()});
			        BufferedReader in = new BufferedReader(
                            new InputStreamReader(
                            pros.getInputStream()));
				    String inputLine;
				
				    while ((inputLine = in.readLine()) != null) 
				        System.out.println(inputLine);
				    in.close();
				
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		
	}
	
}

But I don’t understand
grab and read the STDERR/STDOUT from the process, so they will don’t suffer from full buffers, blocking WinRAR

you will need to read the error stream and output stream in a seperate thread as currently you are not actually running the process!

in pusedo code:


Consumer errConsumer=new Comsumer(pros.getErrStream());
Consumer outConsumer=new Comsumer(pros.getOutStream());

(new Thread(errConusmer,"ErrConsumer").start();
(new Thread(outConusmer.,"OutConsumer").start();

boolean result=pros.waitforexit();

if (result!=0) System.out.println("process failed");




class Consumer imlements Runnable
{
  // this is where you read in and postenially display from the stream;
}


I’ll have to try this tomorrow. I am really thankful that no one has just spat the code at me, it’s nice thinking every once and awhile :stuck_out_tongue: