Is there a function like ShellExecute in Java? - java

Im just looking for function that can run some .exe files
But I didn't find that function in Java
ShellExecute(Operation, File, Parameters, Directory, ShowCmd : String) is there a function like that?

java.awt.Desktop provides some of that functionality:
browse can be used to open the default browser for a specific URI
open opens a file for viewing (with the default viewer of the OS)
edit opens a file for editing (with the default editor of the OS)
...
If you want to run arbitrary exe files, however, then Runtime.exec is the way to go (but using it can be tricky, as you need to consume all streams for it to not lock up).

Related

How to store a file in Java, which is generated on execution of a .exe file?

I have a .exe file, which produces certain files when made to run :
The files produced are WatchDataTest, ngrtgs.test, shubhangi, slatey (as they appear in the image)
I want to run the .exe file through a separate Java Program and obtain reference to the above files. How can this be done?
My point of view: I think, obtaining an OutputStream(wrapped by ObjectOutputStream) on the Process object of this executable can be used to read the objects (files, in this case). However, I am not sure in what way does this executable provides reference to the files produced. Other than that, I have a confusion whether the GUI display is part of the output. I mean does the OutputStream of this executable include the GUI object, which displays on the screen? If not, what all is the output of this .exe?(Pretty confusion here)
The .exe file calls your OS native functions to create those files. You cannot catch that from Java.
If you want to read the content of those files from Java, find them in the directory structure, and open them for reading with the normal Java File I/O API.
I think you want to access those certificates, right? Most likely they're not stored in separate files, but in one file called keystore. In this case I recommend to use Java PKI API or tools to manage your keystore.

Open mp3 file using specific applications java

I want to know if there is a way of opening mp3 files using either Windows Media Player and VLC Media Player.
I have the path of the file saved as a String and was wondering if I can use this to open the file on either of the applications mentioned above.
I understand one method of doing this is by using the Desktop class but this opens the file using the default application, which is Windows Media Player in my case. How would I open the file in VLC media player?
try
Runtime.getRuntime().exec("<your vlc path> <your file>");
I.E.
Runtime.getRuntime().exec("\"C:\\Program Files (x86)\\VideoLAN\VLC\\vlc.exe\" abc.mp3");
Tried and it works
Judging by the Desktop.open apidoc and
Desktop tutorial it looks like the open method can only open the file in their default associated program.
So, you have (maybe) two options here:
access the registry and change the default association (not a nice one)
might try giving a URI and opening it via browser, which might result in opening the file in desired program. However, it looks like the file scheme is the only acceptable solution, and that will most probably open in the default program. There is a mms scheme but it is used for video-streaming, and again, it will most probably open in the default program.
Here is a link to the Runtime.exec solution for windows, linux and osx variant.
You should know the path to the VLC and/or WMP. Then you can use Runtime class for this.
Process p = Runtime.getRuntime().exec("c:/vlc/vlc.exe " + mp3FileName);

Open With... a java program

I want to know how to make a java program that can be used to open stuff up. Ex: notepad++, win zip.... Do I have convert the jar to .exe first? Also, does the file chosen get passed in to String[] args?
By the way, I know that it works with cmd but thats not what I'm asking.
Depends on the OS. Under windows, you need to attach some details into the registry.
Have a look at the 3rd answer in Utilising a file association in a Java application for an example?
You could also have a look at http://www.rgagnon.com/javadetails/java-0592.html
UPDATE
Also, when the OS executes the program, you should receive the file as a command line parameter through the main method
I don't know if this will work suit your needs or not, but you could also take a look at File association in Mac
There's many choices on how to make a Java program runnable. Like you mention, the simplest choice is to use the command line. If you want to make it work with most OS's GUI interfaces (and the Open With dialog) the easiest choice is to make an executable jar. IDEs can make this very easy for you, in Eclipse just right-click on the project and select Export > Java > Runnable JAR file.
Another excellent option is to turn your application into a Java Web Start application, which lets users easily run Java programs being served up online.
Alternatively, like you mention, you could convert it into an .exe file:
Compiling a java program into an executable
How do I create an .exe for a Java program?
How can I convert my Java program to an .exe file?
Deploy the app. using Java Web Start.
JWS provides many appealing features including, but not limited to, splash screens, desktop integration, file associations, automatic update (including lazy downloads and programmatic control of updates), partitioning of natives & other resource downloads by platform, architecture or Java version, configuration of run-time environment (minimum J2SE version, run-time options, RAM etc.), easy management of common resources using extensions..
Here is a demo. of the file services in which the app. is associated with the file type .zzz.
..does it get passed via the windows file chooser?
No. It gets passed to the main as either -open filename or -print filename. What the app. does with those strings is up to it. The demo. linked above will prompt the user in the sand-boxed version, simply because it is sand-boxed. The other one should work without showing prompt or dialog.

Program's executable file to image in Java

I know this might seem a bit odd, but i have this requirement and I want to know if this is possible.
I have a java swing application that I select a file (program) and this program is added to a list. When the list is completed, i execute the list of programs (this is like a start-up manager).
What I want to do is somehow, grab the file that I select and display it as image to my UI. For common files like pdf, doc, txt this is easy, I just have a generic image for each type. But lets say I want to execute regedit.exe or msconfig.exe, I want to be able to grab its icon (picture below) .
Does anyone know how this can be done?
Thanks
Take a look at FileSystemView.getSystemIcon(File).
It's a little limited (in that you will only get one size), but it's build in and doesn't require any additional libraries or JNA or JNI even...
File f = new File(...);
Icon icon = FileSystemView.getFileSystemView().getSystemIcon(f);
If you want the native icons you need JNI. Java has no default API for fetching the native icons in different sizes. Here are some startingpoints for windows & linux:
File icon overlay in java for windows
How do you get the icon, MIME type, and application associated with a file in the Linux Desktop?
If you do not need the exact native icons you can get the mimetype of the file and set a icon on your own:
Getting A File's Mime Type In Java

How can I determine the way to access certain third-party programs and resources from my Java app?

I'm trying to open a PDF file after I generate a report. I mean, the user logs in (it's a Swing-based app) and clicks to generate a report. Then, a PDF file is generated. I would like to launch the PDF reader at that moment. I could do something like exec("evince "+path_to_pdf_file). It's just for Ubuntu, Windows would be more difficult. I'm thinking I need to explore the registry.
How can I achieve this?
What you need is the method java.awt.Desktop#open
Launches the associated application to open the file.
If the specified file is a directory, the file manager of the current platform is launched to open it.

Categories

Resources