We help IT Professionals succeed at work.

Executing java in a batch file

rahman
rahman asked
on
Medium Priority
873 Views
Last Modified: 2008-02-01
Hi,

I am trying to execute a java program from within a batch file. Here is the problem description:

I have jre installed under c:\program files\install\jre\jre1.3 directory (note the space in the path) and this is the simplified  content of the batch file:

-------------------------------------
@echo on
set command=cmd.exe /C start c:\program files\install\jre\jre1.3\bin\java -Dproperty=true com.test.pathtest
cmd=%command%
-------------------------------------

Now, I am having trouble executing the batch file, because it doesn't like the space in java's path. If I wrap around it with double quotes, it doesn't like the property "-DHTTPClient.dontChunkRequests=true" complaining that this file isn't found. What is the right way to escape the space this path is the batch file ( "" or \"\" didn't work for me)?  

Thanks.

Comment
Watch Question

Commented:
you should wrap c:\program files\install\jre\jre1.3\bin\java in quotes i.e.
"c:\program files\install\jre\jre1.3\bin\java"
and the value of any properties if they have quotes i.e.
-DHTTPClient.dontChunkRequests="true" not
"-DHTTPClient.dontChunkRequests=true"
I believe that would work.
A good example I always use it the startup files in Tomcat (or Catalina now) look at catalina.bat file and it basically does what you want.
http://jakarta.apache.org
hope this helps

Author

Commented:
Thanks for your help. Here is a simplified version:

cmd.exe /C start "c:\program files\install\jre\jre1.3\bin\java" -DHTTPClient.dontChunkRequests="true" com.test.pathtest

If I execute the above line from a command prompt, I get the following error message:
 Can not the find the file '-DHTTPClient.dontChunkRequests=true' [or one of its components] ....

Clearly I am not passing the parameters correctly. It doesn't have any problem with the space now, but properties are not recognized.  Any additional help will be greatly appreciated.

Author

Commented:
Thanks for your help. Here is a simplified version:

cmd.exe /C start "c:\program files\install\jre\jre1.3\bin\java" -DHTTPClient.dontChunkRequests="true" com.test.pathtest

If I execute the above line from a command prompt, I get the following error message:
 Can not the find the file '-DHTTPClient.dontChunkRequests=true' [or one of its components] ....

Clearly I am not passing the parameters correctly. It doesn't have any problem with the space now, but properties are not recognized.  Any additional help will be greatly appreciated.

Commented:
I tried it and had the same problem. The only suggestion I have is to put the command in another bat file and call that file i.e.
cmd.exe /C start test2.bat
and in another file:
cd "c:\Program Files\java\jdk1.3\jre\bin"
.\java.exe -DHTTPClient.dontChunkRequests=true com.test.pathtest

not sure if that helps

Author

Commented:
It doesn't really help me much, as I really need to keep it within one file. Do you know of any way or any method in java (i know it's unlikely) where I can pass in a path or string such as "c:\program files\test" and get a return string such as "c:\progra~1\test" back? I create this batch file on the fly and any method like that could help me solve this problem. Any idea will be appreciated.

Thanks.
Commented:
well you could try one more thing, you could try to add the bin directory to the path and then call java i.e
SET PATH=%PATH%;"c:\program files\install\jre\jre1.3\bin\java"
cmd.exe /C start /i java -DHTTPClient.dontChunkRequests="true" com.test.pathtest

I'm not sure if you need the /i it's basically supposed to Pass CMD.EXE startup environment to the new window.

Author

Commented:
It might be a very good idea. Let me give this a try and I will get back to you once I find out.

Author

Commented:
It worked! Thank you very much for the great idea. :)