Delete file in java - java

I want to delete file on windows OS using java, how ever some time file may be in use by external process, how can I delete forcefully or by knowing which process use that and kill that process or any how, any code or way?

Java doesn't have any built-in tools to find out what process opened specific file. This is OS-specific. You must run some external tools for that, but I don't know any Windows command-line tool allowing you to do that.

You can call low level Windows routines via JNI (http://www.atwistedweb.com/java/jni.html) or JNA (http://jna.java.net/)
But how about a more simple solution: Download Unlocker and run it with ProcessBuilder.

Related

How do i get the working directory of a windows process in java?

I would like to get the path to the working directory of a specific process (for example for the PID of the process). I am Not Talking about the working or current Directory of the process where my Java Code is running. Its a simple task with Linux, but for Windows i cant find a proper solution. Furthermore, it would be nice, if its a Command or a Framework for Java, because i will need the path in my Code. I am not looking for the path to the executable, also Not for a solution with wmic or process explorer.
Already thanks for the help.
I already tried commands like tlist and wmic, but those solutions cant be utilize in my code. I am looking for a solution that i can use without special installations on Windows.
JNI and JNA provide means to call directly into native libraries from Java code, and it is feasible to use these to call out to Windows libraries.
There is a github project that appears to be close to the need: https://github.com/kohsuke/winp. Perhaps you can add the needed code and send up a pull request, or fork the project.
Note that any solution here is going to be windows-specific, meaning the application using it will not run on another platform. Given the nature of the question, that doesn't sound like it would ever be a concern.

How to launch a .jar that needs elevated privileges?

I have a little .jar that executes a simple system administration task and so it needs to be run with elevated privilege. I've been researching this for hours and now know that it can be done in three ways:
1) ran from an elevated cmd prompt
2) convert the .jar to .exe and bundle it with a manifest file
3) use another .jar to launch my .jar and ask for permission.
Option 1) won't work for me because this will need to be deployed to other users that won't know how to do this. Option 2) isn't ideal because I chose to write this app in Java for its portability. This will likely be run on different systems and Java seems the most compatible. So that leaves Option 3) and is where my question comes in. I can't seem to sift through the multitude of info out there on how to accomplish creating a wrapper for my app. With my specs in mind what do you all recommend for creating a wrapper .jar that will prompt the user to allow my .jar to run? Thanks
On Windows, it is possible to run a Java application either as a desktop application, or as a Windows Service in the background. In the case of a Service, the Wrapper needs to be able to be installed, removed, started, stopped, have its status queried, etc. Depending on whether the application has a GUI or is meant to be run in a command window also determines how it will be run.
On Windows systems, the most way of launching the Wrapper is to make use of dedicated batch files to perform each action of controlling the Wrapper. This makes it possible for the end user to double click on the batch file icons or set up links in menus, just have a look if you have the java runtime env.
Nice tutorial: http://wrapper.tanukisoftware.com/doc/english/qna-service.html
Here it has some other possibilities, using Dedicated Batch File, Command Based Batch File or Standalone Binary : http://wrapper.tanukisoftware.com/doc/english/launch-win.html#dedicated
Think you can do this with .bat file. Make sure you have java runtime env, so that you can execute jar file using java -jar command.
If your looking to force the user to use elevated permissions then pure Java isn't going to cut it. I suggest you write native code and use the Java Native Interface (JNI)

How does USB Disk Ejector work?

I have a Java application that is designed to run on removable drives. I would like to add a button to allow the user to safely remove the drive the program runs on as USB Disk Ejector allows. However, I'm not sure how to achieve this (code wise) as the drive cannot be ejected if the program is running from it. I know that this program is open source, but I don't know where to find the code I am looking for and it isn't written in a language I have learnt.
I would therefore very much appreciate it if somebody could help me work out how to achieve this functionality in my Java application. Obviously I don't want to just copy, but the only thing I know at the minute is I have to pass control over to some sort of temporary script that is not on the drive I wish to eject.
Thanks in advance
As far as I know, it is not possible to implement this in pure Java as operations such as ejecting/unmounting drives are operating system-specific and not included in the default Java library which usually only supports the lowest common denominator. You need to execute some platform-specific code either by executing a script/batch file or by running native code written e.g. in C using Java's JNI mechanism.
You're right that you will need to run the application from another drive. I would follow the Java Web Start CD Install guide, which should work just as well for a USB drive or any other media as it does for CDs.
You would need to make your application a Java Web Start application. It's actually much easier than it sounds; your .jar does not need to change, you just create a small XML file with a .jnlp extension and place it next to the .jar file. Information on Java Web Start and JNLP files can be found in the tutorial and in the links at the bottom of that page.
Your external executable which performs the safe removal can be included in your application .jar file. You can copy it from your .jar to a temporary file in order to run it:
Path safeRemovalProgram = Files.createTempFile(null, ".exe");
try (InputStream stream =
MyApp.class.getResourceAsStream("saferemoval.exe")) {
Files.copy(stream, safeRemovalProgram,
StandardCopyOption.REPLACE_EXISTING);
}
safeRemovalProgram.toFile().setExecutable(true);
ProcessBuilder builder =
new ProcessBuilder(safeRemovalProgram.toString());
builder.redirectOutput(ProcessBuilder.Redirect.INHERIT);
Process safeRemovalProcess = builder.start();

Platform independent way to fire off external applications/helpers by file type?

For example, one application that I'm working on stores PDF files into a database, then can pull them back out for display. I've got a call in there using Runtime.exec to do a "cmd /c start " plus the PDF filename. Works great for Windows. Would prefer to find a platform independent way (trying to avoid OS detection with alternate methods for various OS) to do this though as we also run the software on Solaris and Mac.
Look at Desktop which has a open method and that would be platform independent.
Launches the associated application to
open the file.
I'd be interested to see if there is a 'correct' answer for this. If I were to do this, I'd have a properties file mapping of OS to the command needed to run, and then resolve the OS at runtime.
Eg in a properties file:
windows=cmd /c start
mac=open #(I think)
linux=... etc

How to call c++ functionality from java

I have a Java program that is mostly GUI and it shows data that is written to an xml file from a c++ command line tool. Now I want to add a button to the java program to refresh the data. This means that my program has to call the c++ functionality.
Is the best way to just call the program from java through a system call?
The c++ program will be compiled for mac os and windows and should always be in the same directory as the java program.
I would like to generate an executable can the c program be stored inside the jar and called from my program?
If you have access to the code and want an 'interactive' experience with the external program (e.g., make call, get results, make additional calls), investigate JNI, which allows you to call C or C++ code from a Java application by including & linking JNI juice to your C or C++ app with .
See:
http://en.wikipedia.org/wiki/Java_Native_Interface
http://www.acm.org/crossroads/xrds4-2/jni.html
If you really just need a "launch app and get results" sort of solution, check out Runtime.exec(), which lets you launch an external program & capture its output.
See:
http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html?page=1
http://www.rgagnon.com/javadetails/java-0014.html
Assuming no better communication method is available (SOAP, ICE, Sockets, etc), I'd call the executable using Runtime.exec(). JNI can be used to interface directly, but I wouldn't recommended it. No you can't put an executable in the jar. Well you can, but you can't run it, since the shell doesn't know how to run it.
You may also want to look at the Java Native Access API (JNA).
To answer your final question, you can't run an executable from within your jar.
However, you can store it within your jar and extract it to a temporary directory/file prior to running it (check for its presence the first time and extract if necessary). This will simplify your distribution somewhat, in that you only have the jar to distribute, and ensures that you're running an executable that matches your jarred Java code.

Categories