Java help needed

agentrfr

Executive Member
Joined
Jul 8, 2008
Messages
5,419
Reaction score
841
Location
Sandton, Gauteng
Howzit guys

I'm wondering if any of you know how to have a java program execute a windows cmd prompt.

Basically, I've got a php process to append a bunch (around 300 000) of .bin files to one massive .csv file. The php process works great, but I'm trying to make it user friendly for people other than nerds to use. I want to use Java to make a GUI, and then a drop down menu of dates and so on...

Anyway, the java program makes the String I need to run in the cmd prompt, but I want it to execute automaticly in window's cmd.

For example, I want the program to open a cmd terminal, then run the following

Code:
cd \php
php process_dukascopy_data.php EURUSD 200702 201008 EURUSD.csv

and then let that do its thing.

Or if any of you have a handy java based .csv builder lying around, I could use that too :)
 
Last edited:
Put the above into a bat file and then use the below in your java

Code:
 try
           {
final String file = "C:\\Documents and Settings\\All Users\\Start Menu\\Programs\\Accessories\\Calculator"; 
             Runtime.getRuntime().exec("rundll32.exe shell32.dll, ShellExec_RunDLL " + file);

           } catch(Exception   e){
               JOptionPane.showMessageDialog(null,"error msg","ERROR",JOptionPane.ERROR_MESSAGE);
           }

Sorry it's untidy, but don't have time to edit
 
Put the above into a bat file and then use the below in your java

Code:
 try
           {
final String file = "C:\\Documents and Settings\\All Users\\Start Menu\\Programs\\Accessories\\Calculator"; 
             Runtime.getRuntime().exec("rundll32.exe shell32.dll, ShellExec_RunDLL " + file);

           } catch(Exception   e){
               JOptionPane.showMessageDialog(null,"error msg","ERROR",JOptionPane.ERROR_MESSAGE);
           }

Sorry it's untidy, but don't have time to edit

Thanks for the help :)

What I need to to is have the bat file run the second line as a String generated by java.

Code:
cd \php
php process_dukascopy_data.php [COLOR="Red"]EURUSD 200702 201008 EURUSD.csv[/COLOR]

The part in red is what Java generates, and that is where I am having the problem.

Would an easier way of doing it be creating a txt file with the necessary code, renaming it to a bat file then running that?
 
Shouldn't be too hard. I've done this a number of times, and graviti is on the exact right path. Just play around with that code a bit and I'm sure you'll get it. Or just make a different .bat file only with your "line 2" that you want to run. That way you can just run the whole file without getting anything else running that you don't need.
 
OK, so I've had more time to think about it.

The following should work perfectly.

Your batch file:

Code:
echo off
cd \php
php process_dukascopy_data.php %1 %2 %3 $4

Your Java code

Code:
try
{
    //String JavaGeneratedInfo = param1 + " " + param2 + " " + param3 + " " + param4;
    //                  OR
    String JavaGeneratedInfo = "EURUSD 200702 201008 EURUSD.csv";
    final String file = "C:\\path\\runMe.bat " + JavaGeneratedInfo; 
    Runtime.getRuntime().exec("rundll32.exe shell32.dll, ShellExec_RunDLL " + file);
}
catch(Exception   e)
{
    JOptionPane.showMessageDialog(null,"error msg","ERROR",JOptionPane.ERROR_MESSAGE);
}

Basically generate your info, then pass them as parameters to the batch file. You can have up to 9 parameters passed to batch files. The batch file accesses each one via the %num variable seen above. Parameters are separated by spaces.

Hope this sorts you out
 
OK, so I've had more time to think about it.

The following should work perfectly.

Your batch file:

Code:
echo off
cd \php
php process_dukascopy_data.php %1 %2 %3 $4

Your Java code

Code:
try
{
    //String JavaGeneratedInfo = param1 + " " + param2 + " " + param3 + " " + param4;
    //                  OR
    String JavaGeneratedInfo = "EURUSD 200702 201008 EURUSD.csv";
    final String file = "C:\\path\\runMe.bat " + JavaGeneratedInfo; 
    Runtime.getRuntime().exec("rundll32.exe shell32.dll, ShellExec_RunDLL " + file);
}
catch(Exception   e)
{
    JOptionPane.showMessageDialog(null,"error msg","ERROR",JOptionPane.ERROR_MESSAGE);
}

Basically generate your info, then pass them as parameters to the batch file. You can have up to 9 parameters passed to batch files. The batch file accesses each one via the %num variable seen above. Parameters are separated by spaces.

Hope this sorts you out

Absolutely brilliant. Thank you :)
 
Top
Sign up to the MyBroadband newsletter
X