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.
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
Is there any direct support (Asset) to run a batch file in jBPM.
I know that I can run Java code like below.
This is the java code I am trying to run.
ProcessBuilder pb = new ProcessBuilder("C:\\Files\\Test.bat");
Process p = pb.start();
int exitStatus = p.waitFor();
System.out.println("Execution Done. Status: "+exitStatus);
I am curious to if there is a direct way to run a Batch file directly without writing Java code manually.
I am not aware of how to execute the batch file but maybe you can try with
executing the shell script using work item handler.
I have a shared drive (NAS) attached to my Linux server wherein I am able to create and write to file usiing the following Java code.
String filePath = remotePath + fileName;
BufferedWriter fileWriter = new BufferedWriter(new FileWriter(filePath));
fileWriter.write(fileContents);
fileWriter.close();
File file = new File(filePath);
file.setExecutable(true);
file.setWritable(true);
file.setReadable(true);
I have tried to log the permission attribute too using canExecute(), canWrite(), canRead() and all the output are logged as true.
But this newly created file is not inheriting the folder permissions. When user try to access(Read/Delete) files using Linux script it gives permission denied.
The user running the script is the folder owner while the file shows owner as root. Due to policy, the user doesn't have sudo rights. How can I make it accessible?
If i understand this right, then your Java process is running as root. The created file is owned by the user that runs the process. Which is in your case root.
I see two options for you:
Let the Java process that creates the file run as the user that owns the directory. So the files will be owned and accessible by the user.
If the Java process must run as root then you need to change the owner of the file after it had been written. see Change file owner group under Linux with java.nio.Files on how this can be done.
This question already has answers here:
How to check if a file is open by another process (Java/Linux)?
(5 answers)
Closed 6 years ago.
I am trying to write an excel file in java, and saving it in a shared folder. but if the file currently open at any system by anyone, it is throwing an error. So is there any way that we can close an excel file via a java command?
How to delete/close open excel file. so you have to auto close the file before delete...
final File file=new File("E:\\book1.xlsx");
Runtime.getRuntime().exec(
"cmd /c taskkill /f /im excel.exe");
new Thread(new Runnable() {
#Override
public void run() {
try {
Thread.currentThread().sleep(2000);// you need to wait 1-2 sec to close file before delete
file.delete();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
If the file is actually open in someone's window (like you would open a ms word doc), then it's not going to be possible to close the file on their machine from a java program. If you just need to one-time update that file, I'd recommend creating another file with a different name, and then manually replacing the contents of the desired destination with the contents of the file that you programatically create.
Depending on the way the file is opened, you may be able to execute the command net files <id> /close via Runtime.exec(). You can also combine the psexec command to close files on remote computers.
This answer seems to describe a similar situation, you just need to make java run the commands:
https://superuser.com/questions/643916/remotely-close-open-shared-files
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)