Running other binaries or batch files from within java
Hello,
I recently inherited several closed-source windows executables. I need to execute them when a user clicks on a JButton. (after which I can parse the text files they generate)
I realize this will make my application windows-only...
Does anyone know how I can execute a file (or dos shell commands) from inside java?
Thank you in advance,
Andy
My understanding was the command.com (which isn't an executable, just like dir isn't an executable (not a windows user, doesn't know these things))
"Not to be confused with OS/2 Warp CMD.EXE, the file CMD.EXE is the Microsoft Windows NT command line shell and is more compatible and portable between different hardware platforms when compared to the original COMMAND.COM, which has been used as the command interpreter with DOS for several years. COMMAND.COM is included for backwards compatibility and is recommended that it be used when old MS-DOS programs may not be able to be run in Windows NT."
"Apart from what was listed in the opening paragraph, one of the most noticeable things a user is going to notice when using CMD to access MS-DOS is the ability to use long file names. When a user is using COMMAND they will need to use the short 8.3 name. For example, if a user wanted to access "My Documents" in COMMAND they would need to type "cd mydocu~1". However, a user who wanted access the same directory through CMD could type "cd my documents"."
I believe my legacy software falls in to the category of "old MS-DOS programs" (and the Windows XP kernel is based on NT)
Similar Messages
-
Iam not able to run a simple batch file from within java
This is how my code looks like.
It does'nt give me any error but I dont see the output from batch file which is suposed to be a simple redirection of dir command...
package mypackage1;
import java.io.File;
public class RunBatch
public RunBatch()
try{
Runtime rt = Runtime.getRuntime();
String cmd = "generate_invoice";
String path = "C:\\application\\Maps\\Reports";
File file = new File(path);
String[] envp = {""};
String[] args = {"C:\\application\\Maps\\Reports\\generate_invoice.bat"};
System.out.println("************ executing batch ");
Process proc = rt.exec(args);
System.out.println("************ batch executed ");
}catch(Exception e)
e.printStackTrace();
* @param args
public static void main(String[] args)
RunBatch runBatch = new RunBatch();
}Runtime.exec() only can run executable files. Batch file is not an executable - it is a script executed by command interpreter (cmd.exe). So, you should execute "c:\windows\system32\cmd.exe /c C:\application\Maps\Reports\generate_invoice.bat".
Denis
http://www.excelsior-usa.com/jet.html
JVM with AOT compilation -
How to run a batch file from within an Oracle 9i Form?
Hi, I would like to execute a batch file from within an oracle form - I'm assuming there's some PLSQL command I can use? I am not allowed to use any third party libraries, and I'm hoping I can get away without having to progam any C or Java to do it. I don't want to run my java program from my form, but my lecturer is insisting on it.
Can anyone help?
Thanx
AdamCan u give me an example
I need to run a batch files that does a series of step doing dir ( in 3 different folders)
after each one it shows on screen with a pause & user press enter
can this work out ? -
How can I execute a batch file from my java program
Hi,
Can someone help me or direct me to a link,
How can I execute a DOS batch file from my java program?
ThanksYou will need to grab a handle to the process's
outputstream so u can see its output.The OP didn't mention any output from any batch file;
nor any input for that
matter,so lets not complicate matters here for now
ok?Actually I think this is essential to see whether it works or not. It's either that or do some manual check to see whether it ran, which is not exactly elegant, and in some cases this may not be easier than simply writing the output stream code, or in fact it may be impossible to check manually.
I'm sure it wasn't intentional that your post appeared to be bristling with attitude. -
Run a MS DOS batch file from the client
Hi,
I created a java source to run a bat file, the folder is loacted in the Database server.
(Not in application server)
create or replace and compile java source named execcommand as
import java.io.*;
import java.util.*;
public class ExecCommand{
public static void run(String cmdstr) throws IOException, InterruptedException
try {
int rtn;
cmdstr = "cmd /c " + cmdstr;
Process prcs = Runtime.getRuntime().exec(cmdstr);
while (prcs.getInputStream().read() != -1 ) {}
catch (IOException e) {
e.printStackTrace();
I called the java source from the procedure
create or replace procedure run_batfile is
begin
execcommand(cmdtext => 'e:\services\genPDF.bat');
dbms_output.put_line('file created successfully');
exception
when others then
dbms_output.put_line(sqlcode || sqlerrm);
end run_batfile;
But the bat is not executing (The bat file is to generate a PDF file ). Bu when i executing this from my PC (Client PC) directly on the RUN , its generating the PDF file in the server.
What could be the problem, What should be the path in the procedure,
Shall i create any virtual directory ?
Thanks in advance,
RizlyHi Justin ,
Sorry for the late replay.
I am running Oracle on Windows.
The MS DOS Batch file is to convert XML file to PDF format. It is a Java Application.
also it is a batch process, thats why i created a batch file for that. I am using FOP to convert XML to PDF.
FOP is in the e:\services folder of the Database Server machiene. XML file is also in the same forlder and I need to generate the PDF file also in the same machiene.
<<- You have a client machine. What do you mean by "executing this from my PC (Client PC) directly on the RUN , its generating the PDF file in the server.">>
I have mapped the e:\services folder in my client machience, so i am accessible to the genPDF.bat file that is in the 'e:\services' folder of the DB Server. When i run the bat file from my PC as Start > Run e:\services\genPDF.bat . The PDF file has generated in the DB Server machien.
I dont have the copy of the batch file in client machiene, but i have mapped to my client machiene.
Thanks again,
Rizly -
Executing an HTML file from within Java
My program produces a file of run statistics formatted as html (stats.htm).
I want to execute this file in a browser from within my program - ie. call a browser with this file as a parm.
How do I do that ?
Have tried executing a DOS command for the form "cmd /C fullpathname-of-file" but it does nothung.
Thanks
...KenRThank you very much...that worked file.
The relevant code is:
String arg = "G:/tma03/agents/stats.htm";
java.awt.Desktop desktop = java.awt.Desktop.getDesktop(); ...to set-up Desktop object
try ... to create a URI object using arg
java.net.URI uri = new java.net.URI( arg );
desktop.browse( uri ); ... to browse the URI
} -
How to invoke the .bat(batch file ) from the java program
i want to run some commands when i run one java program.
I wrote those dos commands on the batch file and i want to include the bat file in the java program so that i can execute the bat file when i run the java program.
tell me the way that i can run my bat file inside the java program.i tried this :
a .bat file named test.bat, with this code : copy test.bat test2.bat
a java class, Test.class, in the same directory
public class Test {
public static void main(String[] args) {
try {
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec("cmd /c test.bat");
proc.waitFor();
int exitVal = proc.exitValue();
System.out.println("Process exitValue: " + exitVal);
catch (Throwable t) {
t.printStackTrace();
} -
Problem executing .bat file from within Java class
I'm stumped: I have no problem executing a .bat file that sets classpaths and executes a Java jar, but when I try to execute other executables first and then the .jar, my application hangs and since the DOS box doesn't come up over my GUI, I can't see what's going on.
This works:
public void execute() throws Exception {
String s = "c:\\cs47auxs\\omnijar\\omni.bat";
Process p = Runtime.getRuntime().exec("\"" + s + "\"");
p.waitFor();
JOptionPane.showMessageDialog(frame,
"The Database Has Been Successfully Reloaded.",
"Information Message",
JOptionPane.INFORMATION_MESSAGE);
Here's the .bat 'omni.bat'
set JAVA_HOME=c:\j2sdk1.4.2_04\bin
%JAVA_HOME%\java -jar C:\CS47AUXS\OMNILOADJAR\OmniLoad.jar
This doesn't work:
public void execute() throws Exception {
String s = "c:\\cs47auxs\\omnijar\\jobomni.bat";
Process p = Runtime.getRuntime().exec("\"" + s + "\"");
p.waitFor();
JOptionPane.showMessageDialog(frame,
"The Database Has Been Successfully Reloaded.",
"Information Message",
JOptionPane.INFORMATION_MESSAGE);
Here's the .bat file 'jobomni.bat'
SET NETX_HOME=C:\CS47AUXS
SET COBOL_HOME=C:\CS47AUXS\OFFLINE
CD %NETX_HOME%
CALL SET-NETX.CMD
CD %COBOL_HOME%
SSBPPC10 JOBOMNI X
SET JH=C:\J2SDK1.4.2_04\BIN
SET OMNI_HOME=C:\CS47AUXS\OMNILOADJAR
CD %OMNI_HOME%
%JH%\java -jar omniload.jar
Can anyone shed some light here? Even when I execute the application from the command line the new DOS box doesn't become visible nor can I see any errors. If I could just get that visibility, I could probably figure out what is going wrong.Same problem with me as well.... Badly looking for a solution...
I predict the following:
- If your batch file has pretty less number of dos/shell commands then it gets executed fine with exec() and proc.waitFor();
- If you increase the number of dos/shell commands in the bat file then try executing it then it definately hangs at proc.waitFor();
Even "cmd.exe /C C:\\test.bat" hangs... if the commands are more...
Is this some sort of bug? or am i doing anything wrong? I tried searching for solution on the net and search forums... but couldnt find a solution for the same.. not sure where i missed, what i missed...
Incase some one finds a solution.. do post it here...
Message was edited by:
amadas -
Error running batch files from java source file???
Dear Friends,
hi,
this is with response to a doubt i had earlier ,
i want to run batch files from the java source file ,i tried using this code (here batrun is the batch file name that contains commands to run other java files)
try
String [] command = {"c:\\vishal\\finalmain\\batrun"};
Runtime.getRuntime().exec(command);
catch(Exception e)
but i got the following error.
java.io.IOException: CreateProcess: gnagarrun error= 2
plz. help me, i tried all combination w/o success,
in anticipation(if possible give the code after testing)
Vishal.hello there,
i solved the prob. by using
cmd /c start filename ,but i need to pass parameters ie
cmd /c start java "c:/vishal/runfile a b" where a and b are the parameters. but it is not accepting this in Runtime.getRuntime.exec(),
any solutions ?????????
regards,
Vishal -
Running batch files from Java using exec method
Hi,
I want to run a batch file from my Java program like this:
try {
Process proc = Runtime.getRuntime().exec("C:\\Refresh.bat");
catch (Exception e) {
MessageBox.show(e.getMessage());
Refresh.bat file contains two commands.
First one unzips certain zip file.
Second one refreshes a SQL Server database using osql utility.
Problem is that when program is run it executes only the first command and hangs on the second one.
Please help.
TIA
RavinderFrom the FAQ:
2. How do you launch an external program on a Microsoft Windows platform from a program developed on the Java [tm] programming language?
The following will launch notepad in Microsoft Windows NT:
Runtime.getRuntime().exec("cmd /c notepad.exe");
To launch a program in Microsoft Windows 95/98 use:
Runtime.getRuntime().exec("c:\\windows\\notepad.exe");
The Runtime class allows interaction between a program and its environment. The first string command instructs the command line interpretor, cmd to open up the calculator application.
The exec() methods do not use a shell; any arguments must have the full pathname to the shell as well as the command itself.
For example, to run a shell on the UNIX� platform, type:
Runtime rt = Runtime.getRuntime();
Process p = rt.exec("/usr/bin/sh -c date");
To run a batch file under Microsoft Windows 95/98:
Process p = rt.exec("command.com /c c:\\mydir\\myfile.bat");
To run a batch file under Microsoft Windows NT:
Process p = rt.exec("cmd /c c:\\mydir\\myfile.bat");
where 'cmd' and 'command.com' are the command-line interpreters for Microsoft Windows machines.
The Runtime.exec() methods might not work effectively for some processes on certain platforms. Special care should be taken with native windowing, daemon, WIN16/DOS process or some shell scripts.
regards,
jarshe -
Run remote batch file from Web Application Server
Hello,
I've developyed a Java program and deployed it on WAS v7.1.
My java program tries to run a remote batch file which is located on remote Windows file system (and not on the WAS file system itself).
The batch file was launched on the remote server (I saw it on the Task Manager of the remote server), but the remote batch file actually wasn't run (it was stuck).
In order to run the remote batch file from my java program that is deployed on the WAS I've created a batch file on the WAS file system which its contnet is:
cd c:\jobs
psexec.exe
iltlvt40 -u SAPServiceCE1 -p a2i2000! c:\automation\DiskSpace\runDiskSpace.bat
The jobs folder contains the psexec.exe file (you can find more details about the psexec on this link: http://technet.microsoft.com/en-us/sysinternals/bb897553.aspx).
iltlvt40 is the remote server.
SAPServiceCE1/a2i2000! is a user/password on the remote server which has a full permissions.
My java program calls this batch file using the Runtime.exec() java method in order to run the remote batch file.
Please guide me how to solve this problem?
Best regards,
AhmadHi,
To which batch file you have meant to add some commands: for the local one or remote?
I've added to the local one some different commands and it's works.
I think there is a permission problem when we are trying to go out of the WAS file system.
Best regards,
Ahmad -
URGENT:- Running a batch file from java program
Author: pkanury
I am trying to execute a batch file from my java program using RunTime and Process . It can execute any dos command except for a batch file. Can anyone throw
some light ?? My code looks like this .....
cmd = "command.com /c X:\\grits\\scripts\\test.bat"; Runtime rt = Runtime.getRuntime(); Process p = rt.exec(cmd);
The weird part is that p.waitFor() returns a status of 0 implying that the cmd has been executed successfully. But that is not the case. And my batch file is as simple as - type "ADADA" > junk.txt
Any help would be appreciated.I think it should work when you use a String[] array
instead of a single String:
String[] cmd = { "command.com", "/C", "D:\\batch\\do.bat" };
Note: If the batchfile creates any output (i.e. files),
they will be stored in the directory of the application
which calls the batch file, not in d:\batch\... -
Calling a batch file from PL/SQL
Is there a way to call a windows batch file from within a stored
program in Oracle?
I need to check for error records in a table and if exists, I
would like to call a batch file that invokes mail server and
sends an error notification to a designated person..
Thanks in advanceRe: Upgrate problem
-
How to pass arguments to a batch file from java code
Hi
I have a batch file (marcxml.bat) which has the following excerpt :
@echo off
if x==%1x goto howto
java -cp C:\Downloads\Marcxml\marc4j.jar; C:\Downloads\Marcxml\marcxml.jar; %1 %2 %3
goto end
I'm calling this batch file from a java code with the following line of code:
Process p = Runtime.getRuntime().exec("cmd /c start C:/Downloads/Marcxml/marcxml.bat");
so ,that invokes the batch file.Till that point its ok.
since the batch file accpets arguments(%1 %2 %3) how do i pass those arguments to the batch file from my code ...???
%1 is a classname : for ex: gov.loc.marcxml.MARC21slim2MARC
%2 is the name of the input file for ex : C:/Downloads/Marcxml/source.xml
%3 is the name of the output file for ex: C:/Downloads/Marcxml/target.mrc
could someone help me...
if i include these parameters too along with the above line of code i.e
Process p = Runtime.getRuntime().exec("cmd /c start C:/Downloads/Marcxml/marcxml.bat gov.loc.marcxml.MARC21slim2MARC C:\\Downloads\\Marcxml\\source.xml C:\\Downloads\\Marcxml\\target.mrc") ;
I get the following error :
Exception in thread main java.lang.Noclassdef foundError: c:Downloads\marcxml\source/xml
could some one tell me if i'm doing the right way in passing the arguments to the batch file if not what is the right way??
Message was edited by:
justunme11 - create a java class (Executer.java) for example:
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class Executer {
public static void main(String[] args) {
try {
for (int i = 0; i < args.length; i++) {
System.out.println(args);
Class<?> c = Class.forName(args[0]);
Class[] argTypes = new Class[] { String[].class };
Method main = c.getDeclaredMethod("main", argTypes);
// String[] mainArgs = Arrays.copyOfRange(args, 1, args.length); //JDK 6
//jdk <6
String[] mainArgs = new String[args.length - 1];
for (int i = 0; i < mainArgs.length; i++) {
mainArgs[i] = args[i + 1];
main.invoke(null, (Object) mainArgs);
// production code should handle these exceptions more gracefully
} catch (ClassNotFoundException x) {
x.printStackTrace();
} catch (NoSuchMethodException x) {
x.printStackTrace();
} catch (IllegalAccessException x) {
x.printStackTrace();
} catch (InvocationTargetException x) {
x.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
2 - create a .bat file:
@echo off
java -cp C:\Downloads\Marcxml\marc4j.jar; C:\Downloads\Marcxml\marcxml.jar; Executer %TARGET_CLASS% %IN_FILE% %OUT_FILE%3 - use set command to pass variable:
Open MS-DOS, and type the following:
set TARGET_CLASS=MyTargetClass
set IN_FILE=in.txt
set OUT_FILE=out.txt
Then run your .bat file (in the same ms dos window)
Hope that Helps -
It is possible to execute a batch file from a business service
Hi,
I am developing on inbound batch interface. The interface will be trigged by a batch file and I want to invoke that batch file from within Siebel. To this I've written a business service that uses the Clib.System() method to launch the batch file but I don’t know how don’t send row id as an input with that.
Please correct me below script is correct or not,
var ret = Clib.system(“C:\\SEA752\\siebsrvr\\Batch\\CreditCard.exe \I “ + sRowId);
Regards,
Sankar.PHi,
You might want to post this in [Siebel Forum|http://forums.oracle.com/forums/category.jspa?categoryID=151]
-Arun
Maybe you are looking for
-
No sound from external speakers in win7 partition
hi all, apologies if this is a common problem - but i couldn't find any info specific to it on the web. i installed windows 7 in 64bit w/ bootcamp on my macbook pro and so far everything has been going great .. other than that i can't get any sound o
-
HP Omni 220-1050xt Monitor goes blank at random times and will not light up.
Recently my computer screen starting going blank without any warning. One minute it is fine and I am working away, then the screen turns black and no key sequence will bring it back. I have selected a power scheme that has never, never, never set i
-
Apple TV3 and TV2 will not update to 5.2
Same problem that seems to happen with every update. Brand new Apple TV is stuck at "Step One of Two." Same with old Apple TV 2. It will not update via wi-fi or ethernet cable. It will not update via iTunes. iTunes reports that the downloaded file is
-
PSE10: how to download Raw files from Canon camera? [was:HELP]
How do I download RAW images from a connon camera on photoshop Elements 10??
-
How does the RoboEngine Search Function Work?
When I perform a keyword search via the RoboEngine, the results include .doc, .xls, .pdf files. I thought the RoboEngine performed a keyword search through the HTML pages. Was I wrong in that assumption? Thxs.