Finding and opening any .exe file in C:/ - java

I'm trying to create a small Java program that allows you to ask it to open an .exe file in a java window I created.
Here's a small example of what I want
User: Open chrome
Program: starts looking for a file called chrome.exe in C:/ and opens it
And that for any .exe file.
Is there any way to achieve this in Java?
Thanks!

exec(String command, String[] envp, File dir)
Executes the specified string command in a separate process with the specified environment and working directory.
command is the location of the .exe
envp can be null
dir is the directory of your .exe
With respect to your question it should be...
Runtime.getRuntime().exec("c:\\program files\\chrome\\chrome.exe", null, new File("c:\\program files\\chrome");

Process p = Runtime.getRuntime().exec("cmd /c start "+file.getAbsolutePath());
how to run the exe file
and to find files(search) have a read at Java: Find .txt files in specified folder

Related

Run a jar file using scheduled powershell and validate the result

I am stuck in a scenario where I need to run a jar file which decrypts an encrypted file on regular interval from a particular directory on a windows server. Anyone can help in powershell which executes the jar file ?
I use the following command to run the jar file using command prompt and it works fine on local machine.
java -cp PGPDecrypt.jar pgpDecryptPackage.PGPDecrypt C:\Users\anirudgu\Desktop\abcd\TestFile.pgp C:\Users\anirudgu\Desktop\abcd\anirudguprivatekey.asc C:\Users\anirudgu\Desktop\abcd\outputfile.txt Passcode
This jar takes 4 parameters as input
File Path of encrypted file
File path of private key
File path where we need to put the output i.e. decrypted file
The passphase
The output of the is basically a string : File decrypted successfully and placed at location : C:\Users\anirudgu\Desktop\abcd\outputfile.txt
Can someone please suggest on the powershell side ?
This should work in powershell:
cmd.exe /c 'java -cp PGPDecrypt.jar pgpDecryptPackage.PGPDecrypt C:\Users\anirudgu\Desktop\abcd\TestFile.pgp C:\Users\anirudgu\Desktop\abcd\anirudguprivatekey.asc C:\Users\anirudgu\Desktop\abcd\outputfile.txt Passcode'
The '/C' argument, executes a cmd session and then terminates.
For creating a scheduled script:
https://community.spiceworks.com/how_to/17736-run-powershell-scripts-from-task-scheduler

Opening a PDF in java using Linux Terminal

I'm trying to open a PDF file in Linux with the xdg-open command in java.
String[] command = {"xdg-open","\""+path+"\""}
Process p = Runtime.getRuntime().exec(command,null);
p.waitFor();
When I run the code in terminal nothing happens even tho if I type it in terminal:
xdg-open path
it opens the PDF.
Any ideas whats wrong?
You should not escape the path: if the program was called, it was with an invalid path ("path" and not path).
String[] command = {"xdg-open", path}
The Runtime.getRuntime().exec(command,null); will use ProcessBuilder internally which, in the case of Linux, should invoke the system command execve.

Calling batch file from Java shows another directory

I am trying through Java to call a batch file in another folder directory.
String cmd = "cmd /c start /wait " + backupFolder + "\\script_encrypt.bat";
Process p = Runtime.getRuntime().exec(cmd);
p.waitFor();
However, when the batch file runs, it shows me the current directory which the batch is not there, why?.
Are you sure the batch file path and the path called from Java are the same?
According the screenshot from Eclipse and the screenshot from Cmd, they are not matching.
I have done the same, and worked for me.

Running a .jar file in a command prompt from double click

I'll start of by saying Im on windows 7.
I have created a .jar file which executes fine from the command line using the - java -jar myJar.jar approach
But what I'm after is to be able to double click on the jar file and for it to open up the command prompt window and run in the command prompt as if i've just typed the java -jar myJar.jar line.
When I double click the jar file I do think it is running because a visual part of the java is appearing, but there is no command prompt window showing my console output.
After looking around I've come across people saying that javaw which is what the jar files are associated with don't have a console and that I need to associate jar files with java.exe instead of javaw.exe. I've tried this and it didn't seem to work.
Can anyone help? A step by step would be nice.
I had the same question and the bat file idea was genius and saved me a lot of time rewriting code. Thanks!(I would have upvoted,but apparently I don't have enough rep.)
Batch (or Bat) files are super easy to make.
Just put the java -jar YourFile.jar into notepad (if you're on windows), save as Title.bat, and put into the same folder as your jar.
presto! a program open-able by the general public.
This is IMHO not possible. You could open the console from the application itself, but that is OS-dependent. If you need the console open, you have to run the application from it as you already do.
If you want to display the command line you have to launch your jar with the command line.
java -jar MyJar.jar
I would do something like this:
(Tested in Widows XP with JRE 1.6, to support other OSs you should verify the path for each OS and select the appropriate console emulator (xterm, gnome-terminal... (check for existance and preference...)))
public static void main(String[] args) throws Exception {
if (args.length == 0) {
String path = Main.class.getProtectionDomain().getCodeSource().getLocation().getPath().substring(1);//Adds extra slash (??) didn't know why
String decodedPath = URLDecoder.decode(path, "UTF-8");
System.out.println(decodedPath);
Runtime.getRuntime().exec("cmd /c start java -jar \"" + decodedPath + "\" actual_run");
}
else {
System.out.println("Hello World");
JOptionPane.showMessageDialog(null, "Hello World");
System.in.read();
}
}
Alternatively I suggest creating a bat file with this content :
java -jar yourjar.jar
This will launch your jar as well as open the command prompt automatically, all from a simple double click on a bat file.
(The bat file needs to be in the same folder as your jar file, else you need to specify the path to the jar, not just the jar name)
This is the easiest solution for beginners:
Open any text editor
write this two lines:
java "yourmainclassname"
pause
save that file as "name".bat
Run it with double click from windows GUI
(of course this new created .bat file must be in the same folder as the .class)
..but there is no command prompt window showing my console output.
No there wouldn't be a console for an executable Jar. You'll need to put that output in the GUI.
Check your MANIFEST.MF
Extract your "executable" jar file into a folder
find MANIFEST.MF in META-INF folder
check presence of this field:
Main-Class: YourMainClassHere
If this field dissapeared then open your original MANIFEST.txt for this point:
Main-Class: YourMainClassHere must end with a new line or carriage return
Warning: The text file must end with a new line or carriage return. The last line will not be parsed properly if it does not end with a new line or carriage return.

Execute a .app file from within a java program

I need to execute an external program from within my java application. I'm programming on a mac and have a .app application I'd like to run when the user selects it. It runs successfully on windows using:
String cmd = "path_to_executable\program.exe\"";
Process p = Runtime.getRuntime().exec(cmd);
...
But this does not work with an .app file. I've opened the contents of the .app file and have found a Unix executable file along with a other supporting files for the appliation. Is the unix executable file equivalent to an .exe file ?
Launching an Mac application from Java on OS X is just a matter of invoking the built-in open command with the Application as a parameter. The open command line tool in OS X knows how to correctly open many different file types, including application bundles. If you were trying to launch the TextEdit.app application, you would invoke:
open /Applications/TextEdit.app
In Java, you would use:
String cmd = "open /Application/TextEdit.app";
Process p = Runtime.getRuntime().exec(cmd);
Yes, the executable that you should be running is the unix executable located in the .app bundle. It might not work properly though, you might need to cd into the directory or something.
Most application binaries are located in Contents/MacOS/App Name. You could make a method which would take in a string name and execute that binary based on the standard directory.
A .app file is really just a folder. It is a way to self contain applications on a Mac. The actual binary file is, most likely, the Unix executable you found. It would help to know some details about the app.
The binary files for a .app are located in Application.app/Contents/MacOS

Categories