Java-Gaming.org Hi !
Featured games (81)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (119)
games submitted by our members
Games in WIP (575)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Using winRAR with Java (help)  (Read 2401 times)
0 Members and 1 Guest are viewing this topic.
Offline Xizor

Junior Newbie





« Posted 2008-01-12 06:57:04 »

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.
1  
Runtime.getRuntime().exec(new String[] {"cmd.exe", "/c", "start", "rar a compressed text.txt"});



also tried
1  
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?
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 818
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #1 - Posted 2008-01-12 09:03:43 »

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 ?

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline Xizor

Junior Newbie





« Reply #2 - Posted 2008-01-13 04:08:58 »

1. Works in command line

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

Here's the code:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
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.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline CaptainJester

JGO Knight


Medals: 12
Projects: 2
Exp: 14 years


Make it work; make it better.


« Reply #3 - Posted 2008-01-13 04:56:50 »

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.

Offline Xizor

Junior Newbie





« Reply #4 - Posted 2008-01-13 08:11:13 »

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
1  
[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  Grin

<edit>

Now the code looks like
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
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
Offline moogie

JGO Knight


Medals: 13
Projects: 6
Exp: 10 years


Java games rock!


« Reply #5 - Posted 2008-01-13 22:20:35 »

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:

1  
2  
3  
4  
5  
6  
7  
8  
9  
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");

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



Offline Xizor

Junior Newbie





« Reply #6 - Posted 2008-01-14 08:20:23 »

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 Tongue
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Longarmx (33 views)
2014-10-17 03:59:02

Norakomi (25 views)
2014-10-16 15:22:06

Norakomi (24 views)
2014-10-16 15:20:20

lcass (26 views)
2014-10-15 16:18:58

TehJavaDev (50 views)
2014-10-14 00:39:48

TehJavaDev (50 views)
2014-10-14 00:35:47

TehJavaDev (40 views)
2014-10-14 00:32:37

BurntPizza (63 views)
2014-10-11 23:24:42

BurntPizza (36 views)
2014-10-11 23:10:45

BurntPizza (74 views)
2014-10-11 22:30:10
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!