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
Related
This question already has answers here:
Executing a shell script inside a jar file. How to extract?
(4 answers)
Closed 2 years ago.
I've created a .jar in which I have a script named init.sh
I got this error.
tim#TS:~/Desktop$ java -jar test.jar
java.io.IOException: Cannot run program "/init.sh": error=2, No such file or directory
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1048)
at java.lang.Runtime.exec(Runtime.java:621)
at java.lang.Runtime.exec(Runtime.java:486)
at Main.run(Main.java:42)
My code which works when I run on Eclipse is below.
String[] cmd = {"src/init.sh"};
try {
Runtime.getRuntime().exec(cmd);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
I've tried to change it without success like that.
String[] cmd = {"init.sh"};
I looked at my .jar file and the init.sh script is at the same level as the Main.class.
My question is : How must I change my code if I want to get a functional .jar ?
Based on the answer here Executing a shell script inside a jar file. How to extract?, looks like the shell script needs to be extracted to a location outside the jar file before you can execute it.
I'm making a program for mac that has it's own file extension .edul. I got it working so that when the program has already opened and you open the file it's load it, but when I try this when the program is yet running I don't get the event and it's not in the args.
I tried to change to com.apple.eawt.Application but unfortunately java.desktop doesn't export this class so this didn't work.
Desktop desktop = Desktop.getDesktop();
desktop.setOpenFileHandler(e -> {
try {
FileWriter fileWriter = new FileWriter(System.getProperty("user.home") + "/Desktop/test.txt");
fileWriter.write(e.getFiles().toString());
fileWriter.close();
} catch (IOException ignored) {
}
});
So I expect a file on my desktop that has the location of the file I opened before the program was running and when it's running and I open another file it's also shows on my desktop.
So after first file open:
[file/path/test.edul]
And on seconds file open:
[file/path/test2.edul]
I'm currently only getting:
[file/path/test2.edul]
So, the problem is that when you define a FileOpenHandler in the class which starts your fx application the FileOpenEvent is only thrown when the program is already running. So you should make a other main class that doesn't extends Application and the FileOpenEvent will be correctly thrown even when the application is still starting up.
I wrote a Java program some time ago that automatically opens a webpage before I get to work in the morning. I do this by calling a batch file from Java, which opens the webpage. This program worked great for about three or four months without any problems, but one day it just stopped working. I have tried opening the webpage from within Java as well instead of the batch file, but I consistently run into a problem. This is the process I use:
I exported my program as a runnable JAR and converted it to an EXE using Launch4j. In my Windows task scheduler I schedule it to run at 5:30am every morning.
My Java program calls the batch file that is stored on my desktop
The batch file opens Chrome and calls the webpage to open
When I run the Java program EXE manually (double-click from the desktop) it runs correctly, calls the batch file, and opens the webpage. However, when I try running the EXE from the Windows task scheduler it does not (visibly) open the webpage. It does appear to run Chrome in the background (according to the task manager/Process Explorer) but the webpage itself doesn't seem to open (I can tell because the webpage I open should be playing music, but I hear nothing).
This is the Java function I use to run the batch file:
public static void openWebpage() throws Exception {
String[] startupBat = {"cmd", "/c", "start", "/B", System.getProperty("user.home") + "\\Desktop\\WebpageStart.bat"};
ProcessBuilder pb = new ProcessBuilder(startupBat);
try {
webpage = pb.start();
} catch (IOException e) {
logWriter.write(tf.format(new Date()).toString() + " - Unable to start webpage");
}
if (webpage != null) {
try {
webpage.waitFor();
} catch (InterruptedException e) {
logWriter.write(tf.format(new Date()).toString() + " - Webpage startup interrupted");
} finally {
webpage.destroy();
}
}
}
And here are the contents of the batch file I use to start the webpage:
#echo off
start "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" http://www.myWebpage.com
Once again, everything works fine when I run the Java EXE manually, but when I try to run it from the task scheduler the webpage doesn't appear to open. What could cause this sort of behavior? What changes when the task scheduler runs a program vs. when it is manually run?
A few things to check
Which user is the scheduled task running as? Is it the same user as the one double clicking on the icon?
Have you changed your password recently? You might need to update the password on the scheduled task
Go to the windows event viewer, are there any errors in there?
Hi how can I get rid of C drive access with java codes I can do this with manual but I want to do this with java codes
here is my code
File file=new File("C:\\Windows\\b.txt");
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
System.out.println("file not created and");
e.getMessage();
e.printStackTrace();
}
And my exceptions
java.io.IOException: Access is denied
at java.io.WinNTFileSystem.createFileExclusively(Native Method)
at java.io.File.createNewFile(File.java:1006)
at tekrar.Write.main(Write.java:26)`
you need to run the program as an administrator to edit the C:\Windows folder. this is a system folder that you should not access.
You cannot write anything in the system folder without administrator privileges in windows. As I understand you cannot request administrator access from within a Java program and you'll have to RUN the application in admin mode.
See this question for some tips: Run Java file as Administrator with full privileges
P.S. Do you really need to write into the windows folder?
I made a little program and it worked fine, but now. First, it mux the xml chapter file in the mkv file, so we get a muxed mkv file. Some day ago I updated java to 1.7.21 and I think this is the problem why it is not working now. It's a little strange, but when I run in netbeans everything is fine, but when I build and I run the .jar file, it is not working. It create the xml file, but not mux in the mkv file (and because not muxed not delete the xml file). Here is the code: (filename=xml file path; mkv=mkv file path)
public void muxing() {
try {
Runtime rt = Runtime.getRuntime();
Process p = rt.exec("c:\\Program Files\\MKVtoolnix\\mkvpropedit.exe --chapters \""+filename+"\" \""+mkv+"\"");
if (p.waitFor()==0) {
File xmlfile=new File(filename);
xmlfile.delete();
}
}
catch(Exception e) {
System.out.println(e.getMessage());
}
}
The program worked with java 1.6 and I think with 1.7.17 too. Win7 32bit. Sorry for my bad English.
Oracle has made breaking changes to Runtime.exec() in Java 7 update 21 (and 6 update 45).
If the program name contains spaces, you need to specify command and arguments in an array:
Process p = Runtime.getRuntime().exec(new String[] {
"C:\\Program Files\\MKVtoolnix\\mkvpropedit.exe",
"--chapters", "\""+filename+"\"", "\""+mkv+"\""});
Another option is to use java.lang.ProcessBuilder:
Process p = new ProcessBuilder("C:\\Program Files\\MKVtoolnix\\mkvpropedit.exe",
"--chapters", "\""+filename+"\"", "\""+mkv+"\"").start();
As stated by Oracle:
Applications that need to launch programs with spaces in the program name should consider using the variants of Runtime.exec that allow the command and arguments to be specified in an array.
Alternatively, the preferred way to create operating systems processes since JDK 5.0 is using java.lang.ProcessBuilder. The ProcessBuilder class has a much more complete API for setting the environment, working directory and redirecting streams for the process.