I am trying to download a XML file from a FTP server with wget in my Java programm.
I have to wait until it finishes the download.
String command = "WGET -O "
+props.getProperty("xmlFolder")+""+
+ rs.getString("software")
+ ".xml ftp://"+props.getProperty("ftpUser")
+":"+props.getProperty("ftpPasswort")+"#"+rs.getString("xmlPfad");
System.out.println(command);
Process p = Runtime.getRuntime().exec(command);
p.waitFor();
System.out.println("downloaded!");
Without the waitfor() it works perfectly, but with this function it stucks after 2-3 MB are downloaded. Any suggestions?
Have you tried to use the --quiet option for wget?
EDIT 1:
The pipe's write side (child process) might be full.
EDIT 2:
From openjdk-6-src-b20-21_jun_2010
In jdk/src/solaris/native/java/lang/UNIXProcess_md.c (at least for a UNIX system) we can see how Java launches a new child process and how it is using pipe to redirect stdout and stderr from child (wget) to parent process (Java)
Related
I am trying to run a batch file using java. The batch file in turn runs a python program. So i should wait till the batch file is done and then proceed with my program.
Problems facing:
I could not run batch file in background. I am able to run it only via start
Process p = Runtime.getRuntime().exec("cmd /c start c://GCTI//IA/QAART//testercheck.bat");
once the batch file ran, it is not closing automatically.
Batch file
"C:\Python27\python.exe" -i "C:\GCTI\IA\QAART\tester\test_monitor.py" -init "C:\GCTI\IA\EpiPhone\Dispatcher6\init\INIT_Designer_QAART_Dispatcher_Chat.PY" -testlist "C:\GCTI\IA\ASR_QAART\dat files\ChatAutomation\chat.dat" 23
Can you please help me to run this batch ile in background?
You don't need the batch file. You can execute the Python program directly from Java code using class java.lang.ProcessBuilder.
ProcessBuilder pb = new ProcessBuilder("C:\\Python27\\python.exe",
"-i",
"C:\\GCTI\\IA\QAART\\tester\\test_monitor.py",
"-init",
"C:\\GCTI\\IA\\EpiPhone\\Dispatcher6\\init\\INIT_Designer_QAART_Dispatcher_Chat.PY",
"-testlist",
"C:\\GCTI\\IA\\ASR_QAART\\dat files\\ChatAutomation\\chat.dat",
"23");
Process p = pb.start();
int result = p.waitFor();
Refer to other methods in class ProcessBuilder for handling the output of the Python script, for example method inheritIO
I'm executing this command in order to open the log file with default file viewer:
Process p = Runtime.getRuntime().exec("rundll32 url.dll, FileProtocolHandler F:/Download/MSI3ca79.LOG");
I would like to know when the file is closed. Is it possible?
p.waitFor(); // doesn't work because the process is terminated just after the execution.
Solution:
ProcessBuilder pb =
new ProcessBuilder("c:/windows/notepad.exe", "F:/Download/MSI3ca79.LOG");
File log = new File(LogFactory.getLogFactory(TestExternProcess.class).getName()); pb.redirectErrorStream(true);
pb.redirectOutput(Redirect.appendTo(log));
Process p = pb.start();
int exitVal = p.waitFor();
Plainly, it's not possible. I would not go this way.
It depends on a particular app (Notepad) opening file. The system doesn't know in general when an app stops viewing a file. Because what is closing a file? Is it closing a tab in a viewer UI? Or cleaning memory allocated for the file by an app? Removing lock file in case of closing doc, xls files?
In order to do it, you would need to write a program controlling the OS and any app that can view the file.
I'm using java's "Process" command to execute a shell script.
In this shell script, it will break down files (from a folder) into smaller Mbs.
Once the files are being broken down, I will use SMTP to mail over those files via email.
The problem is, my mailing function runs before the shell script could actually finish chopping down my files. In the end, no email was being sent out (because apparently the folder is empty).
Here are my codes:
try {
log.info("Going to run the runtime.getRuntime() to execute tar zip files");
String tarLocation = prop.getProperty("tarzipfile.location") + prop.getProperty("tarshell.name");
Process p = Runtime.getRuntime().exec(new String[] {"cmd", "/c", tarLocation});
p.waitFor(); //This waitfor only waits for process, but not the shell to complete
log.info("exit: " + p.exitValue());
p.destroy();
} catch(Exception e) {
log.info("ERROR: Runtime error failed to tar files", e);
}
robot.process(); //Send out mail
My tar file
cd <my file dir>
tar cvzf - <my other file dir> | split --bytes=4MB - archive.tar.gz
What is a good way to counter this problem besides letting it 'sleep' for a few seconds?
How to use java to log off windows. It could be using some windows own exe through Runtime(),
or and other methods.It would be more efficient if you could provide some good alternative than using external programs like nircmd.exe .
When I use this code,
String shutdownCmd = "rundll32.exe shell32.dll,SHExitWindowsEx 0";
Process child = Runtime.getRuntime().exec(shutdownCmd);
I get
Do this (-l for logoff here, -r for restart, etc.):
String shutdownCmd = "shutdown -l";
Process child = Runtime.getRuntime().exec(shutdownCmd);
or (0 for logoff here):
String shutdownCmd = "rundll32.exe shell32.dll,SHExitWindowsEx 0";
Process child = Runtime.getRuntime().exec(shutdownCmd);
You can also make a simple .bat file if it's just a file you want.
Open notepad, type the code below, save as *all files, call it 'logoff.bat' or something and it should work.
shutdown -l
You could call the shutdown executable from the Runtime.getRuntime().exec(...) method. Check out this Q&A for details on that: How do I shutdown - restart - logoff Windows via a bat file?
I have a problem with a Java program. I uses a tool "ugpc" to get the assembly structure and write it in a textfile. When i start the program under netbeans it writes these files without problem. but when i compile it to a jar file and run this jar, i get the error "Error: Fatal system Error 1020005" written in the proc.getErrorStream(); and no .txt file ist written in the ouput. Also in the ErrorStream appears that the textfile can not be found, but he should write it and not open it. My PC is new installed and before that formatting the tool was running good. I use Java 7. When I manually start the command via cmd it works also fine. We startet the tool under another user account and the same error appears.
Process proc = Runtime.getRuntime().exec("cmd /c " + "c:\\ugs\\nx6\\UGII\\ugpc -a -s2 " +"S:\\Geraete\\L\\0154\\E2\\6169-001000-201_Rev-E2_DWG.prt" + " > " + "S:\\LAUF5\\1\\6169-001000-201_Rev-E2_DWG.txt");
proc.waitFor();