473,657 Members | 2,397 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem with Runtime.getRunt ime().exec when running java in .bat

2 New Member
hey,
in my code i'm using Runtime.getRunt ime().exec() in order to run a .bat file that calls another java program (they communicate with each other using RMI).
when i call:
Process process = Runtime.getRunt ime().exec("cmd /c start C:\\MyFolder\\J avaApp.bat");
the seperate process runs perfectly, but when i add a space to the path:
Process process = Runtime.getRunt ime().exec("cmd /c start \"C:\\My Folder\\JavaApp .bat\"");
then the java.exe refuses to load and all i get is an empty cmd with only "C:\"...

i also tried running this line with a ProcessBuilder, but that didn't work as well.
the .bat file has to be under C:\Program Files, so moving it under a folder without a space in its path is not an option.

does anyone know how can i pass through this problem?
thnx.
Nov 9 '09 #1
3 6988
chaarmann
785 Recognized Expert Contributor
The "C:\\MyFolder\\ JavaApp.bat" is one of the arguments of the command "cmd", so if you put a space there inside the name it is parsed as 2 arguments instead of 1. I am not 100% sure but in this case if I remember right you can use method "Process exec(String[] cmdarray)" of class Runtime, and not "Process exec(String command)"! Just put the command and the arguments all as separate strings into a String array and pass that.
Nov 9 '09 #2
Shayco
2 New Member
i tried this method already and it didn't work.
notice i wrote \"C:\\My Folder\\JavaApp .bat\" which means it's supposed to be read by java as one argument.
by the way if i have a .bat file that contains:
"start /b notpad
start /b calc
start /b java -cp args..."
and i run it like this:
Process process = Runtime.getRunt ime().exec("\"C :\\My Folder\\JavaApp .bat\"");
then the notpad and calc run (so it means that the .bat is being launched) but the java doesn't...
Nov 9 '09 #3
chaarmann
785 Recognized Expert Contributor
So if the .bat is being launched, which you can verify by the calculator pop-up, then you have solved your Java problem with Runtime.exec().
Now you need to solve a second problem, ms-dos related: how to start a java program from a batch file.
Can you please list full command instead of only ellipis (...) and "args", I mean, the real command contained inside your *.bat file instead of an informative "java -cp args...".
Did you already try this line exactly from the command line directly?
if yes, have you used environment variables before that, which are not set if the *.bat is invoked from your Java application? I mean something like CLASSPATH or PATH?
If yes, then set them inside your *.bat file or pass them along with the Runtime.exec() method.

By the way,
starting a cmd inside a cmd, that means a second shell (from your command)inside a first shell (from Java), which you tried in the first version of your posting, is unnecessary and a bad idea. I remember a little that you must use option K, that means "cmd /K ..." to pass arguments to the second cmd.
Also I remember some differences between "command" and "cmd". Just try "command" to see if they matter.
Nov 10 '09 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

1
9032
by: aa | last post by:
Anybody know the solution to trailing space in argument of Runtime.getRuntime().exec(arg), Process p = Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler C:\\Documents and Settings\\XX\\Desktop\\- Copy of TestingFolder -\\jjjj hihi.doc"); where there is 2 space between dash and "Copy of TestingFolder", but after going through Runtime.getRuntime().exec(arg), it chopes off one space and
4
24551
by: Hal Vaughan | last post by:
I never noticed this before, but when I run an external program through Runtime, like this: Runtime rt = Runtime.getRuntime(); System.out.println("Running external command: " + sCommand); try {Process p = rt.exec(sCommand);} catch (Exception e) {System.out.println("Cannot Run Command: " + sCommand + ", Error: " + e);} the external command is asynchronous -- completely spun off as a separate thread or process. Is there any way to...
5
40473
by: JW | last post by:
Hi, I don't seem to get any results from the following use of Runtime.getRuntime().exec(cmd) using Java 1.4 on Redhat linux. Suppose that in the same directory as my java file below, I have files junk1, junk2, junk3, and i want to have the java program delete all of these files.
5
14399
by: Ayesha Ahsan | last post by:
Hi, I use Runtime.getRuntime().exec(command) to make my system call. For Windows based Dos, i add "cmd /c" before I type in my system call. So for example make the system call "dir": String command="cmd /c dir"; Runtime.getRuntime().exec(command); What is the equivalenr of cmd /c in Unix .
2
11072
by: uwnewsgroup | last post by:
When I was using Runtime.exec(String cmd) to run a unix utility (join), and try to get its standard output by using Process.getOutputStream(), it blocks forever. I tried it using Java 1.4 and the results are the same on SunOS 5.8, Redhat8.0, cygwin. I read relevant articles and find the reason is that the output of the process exceeds the buffer allocated for that process. So can I adjust the buffer size (on any
1
6938
by: maya2000 | last post by:
Hello, I searched this java newsgroup and tried all suggestions, but still failed. I keep watch a task manager, but cmd.exe never running. All I want to do is run and pop up the cmd.exe using Runtime on Windows 2000, so I can use it. I tried all below cases. String cmd = "C:\WINNT\system32\cmd.exe";
5
25224
by: chenthil | last post by:
In my Java program I need to call two DOS batch programs namely call.bat and start.bat. First I need to start the batch program call.bat and once that program is completed, I need to call the other batch file start.bat. The piece of code which I am using is: public static void ExecuteScripts(){ try { \\Start the first batch program call.bat Process p = Runtime.getRuntime().exec("cmd /c start .\\scripts\\call.bat"); ...
2
1609
by: yaha1982 | last post by:
Hi, Friends I am trying to one firefox at runtime through java code but it gives error. I don't get why such type of error occures. My code is : try { Runtime.getRuntime().exec("firefox"); } catch (IOException e) { e.printStackTrace(); }
2
3360
by: jerry chapman | last post by:
I am trying to send a command to windows from my java program, and I get an error. The pertinent (?) part of my code follows: public boolean action(Event evt, Object arg) { if (arg.equals("Execute")){ inputText=(String)commandText.getText(); System.out.println(inputText); try{ Runtime rt = Runtime.getRuntime(); //Process cp1=Runtime.getRuntime().exec(inputText);
0
8312
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8827
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8732
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8504
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6169
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5632
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2732
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1959
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1622
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.