Create Windows Executable (.exe) for Java Application
Launch4j is a cross-platform tool for wrapping Java applications distributed as jars in lightweight Windows native executable files. In this post, we will learn making such an executable file for a demo java application.
Step1 ) Create a java application
I am creating a very basic java class which simply displays a frame and some text on it. It also has the main() method which will start the application.
package com.howtodoinjava; import java.awt.Color; import java.awt.Font; import java.awt.Frame; import java.awt.Label; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import javax.swing.JDialog; public class ApplicationMain extends JDialog < private static final long serialVersionUID = 1L; public ApplicationMain() < //Create a frame Frame f = new Frame(); f.setSize(500, 300); //Prepare font Font font = new Font( "SansSerif", Font.PLAIN, 22 ); //Write something Label label = new Label("Launch4j Maven Demo with HowToDoInJava.com"); label.setForeground(Color.RED); label.setFont(font); f.add(label); //Make visible f.setVisible(true); f.addWindowListener(new WindowAdapter() < public void windowClosing(WindowEvent e) < System.exit(0); >>); > public static void main(final String[] args) < new ApplicationMain(); >>
Step 2) Create maven file with launch4j configured in it
I have created a pom file for packaging the application as .exe file. If you feel something unclear, drop a comment.
I have a code example that needs to be automated. The code should retrieve the names of all files in a folder, perform certain operations, and save the output with a modified file name. Now, I have a question about running an .exe file using Java code. The .exe file already exists, and I need to know how to run it.
Running a .exe file using Java
Is there a tutorial or reference available for creating a java code to run an exe file with the use of Java code ? The .exe file is already present.
To obtain the Process instance, either call the exec() method on Runtime or utilize the methods of the ProcessBuilder class.
Process process=Runtime.getRuntime().exec("file.exe");
The most expeditious and effortless approach would be to simply proceed in the manner described below.
Runtime.getRuntime().exec("yourapp.exe");
Additionally, take a look at an alternate method available on http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/ProcessBuilder.html.
An instance is present there. ProcessBuilder offers improved management over the process and its arguments and is presumably more organized and clear, particularly when supplying arguments, but may lead to an increase in code length.
Runtime rt = Runtime.getRuntime(); Process proc = rt.exec("javac");
Run EXE from client side, It works like this, in import table you specify IP address and web resource from where you want to load your library (usualy it’s filled with stuff like KERNEL32.dll USER32.dll etc.) So you need to patch your exe loader and change your library name from eg. MYLIB.dll to \xxx.xxx.xxx.xxx\MYLIB (no extension …
Run exe file on all files in a folder with 4 different options of operation
I need to execute an exe file on multiple files within a folder, and I have four distinct operation options to choose from. I want each file to generate four unique outputs, each with a different name. Can you please provide a detailed explanation since I’m not proficient in scripting? Thank you!
This is the code I would like to automate, as an example.
The task at hand involves retrieving all the file names files from a designated folder, performing a set of operations on them, and then saving the resulting output under a modified file name.
Hello once more, I require some enhancements. The objective is to go through all files in a folder that possess two distinct extensions, and compare their names for resemblance, despite the different extensions. Each file has a corresponding one with a different extension that is very similar. Therefore, two names should exist in separate variables, which can be utilized in the following operation commands. Thank you!
setlocal enabledelayedexpansion set /a num=1 for %%a in (C:\folder\*.*) do ( exefile -p1 1 -p2 2 -i %%a -o %%~na!num!%%~xa set /a num+=1 )
How do you run a .exe with parameters using vba’s shell()?, The below code will help you to auto open the .exe file from excel Sub Auto_Open () Dim x As Variant Dim Path As String ‘ Set the Path variable equal to the path of your program’s installation Path = «C:\Program Files\GameTop.com\Alien Shooter\game.exe» x = Shell (Path, vbNormalFocus) End Sub. Share.
How run .exe file in Inno Setup with parameters
I am attempting to execute a .exe file, which can be run through the command console using the following method:
Execute the file named nameFile.exe and install the driver file using the command «-inf fileDriver.inf».
Within the Inno Setup, my script includes the following:
The error message indicates that the parameters are incorrect. What steps should be taken to execute the exe file with the proper parameters?
Upon reviewing your Exec code, it is necessary to transfer the command parameters to the second parameter of the function call. Consider utilizing this alternative approach:
The function identified as Exec is defined in the following manner:
function Exec(const Filename, Params, WorkingDir: String; const ShowCmd: Integer; const Wait: TExecWait; var ResultCode: Integer): Boolean;
Windows — Run exe file on all files in a folder with 4, It should read all the files (two different extensions) in the folder and match the file names for similarity but of different extensions. Each file has a very similar file but with the other extension. So, two matched file names should be available in two variables that can passed on to the subsequent operation …
Running a .exe file inside java code
In my Java code, I am attempting to execute an .exe file that was generated using MATLAB (a Simulink model). The .exe file generates an output file when I open it separately, as expected. However, when I run the Java code, it opens an command prompt , but it does not produce any outputs. It is unclear if the .exe file is even being executed by the Java code.
package combustionModel; import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.File; import java.io.IOException; import javax.swing.JButton; import javax.swing.JFileChooser; import javax.swing.JFrame; import javax.swing.JPanel; public class GUIInterface extends JFrame < JButton b1 = new JButton(); public static void main(String[] args)< GUIInterface gui = new GUIInterface(); >static class Action implements ActionListener < public void actionPerformed (ActionEvent e)< JFrame frame2 = new JFrame(); frame2.setVisible(true); frame2.setSize(100, 200); final JFileChooser fc = new JFileChooser(); fc.showOpenDialog(null); File file = fc.getSelectedFile(); System.out.println(file.getAbsolutePath()); try < Runtime runtime = Runtime.getRuntime(); Process p = Runtime.getRuntime().exec("cmd /c start "+file.getAbsolutePath()); p.waitFor(); >catch (IOException e1) < // TODO Auto-generated catch block e1.printStackTrace(); >catch (InterruptedException e1) < // TODO Auto-generated catch block e1.printStackTrace(); >> > public GUIInterface() < setVisible (true); setLocationRelativeTo(null); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(400,200); setLayout(new FlowLayout()); JPanel adpanel = new JPanel(); JButton OK = new JButton(); b1.addActionListener(new Action()); adpanel.add(OK); adpanel.add(b1); super.add(adpanel); >>
Try by passing the absolute path
Runtime.getRuntime().exec("c:\\program files\\test\\test.exe", null, new File("c:\\program files\\test\\"));
Process p = Runtime.getRuntime().exec("cmd /c "+file.getAbsolutePath());
Process p = Runtime.getRuntime().exec("cmd /c start "+file.getAbsolutePath());
Process — How to get java getRuntime().exec() to run a, example: tesseract «C:\Program Files (x86)\Tesseract-OCR\doc\eurotext.tif» «C:\Users\Dreadnought\Documents\TestingFolder\out» the first argument calls the tesseract program, the second is the absolute path to the image file and the last argument is the path and name of what the output file …
How to run a system application (executable) from Java
Learn how to run an executable from Java using the process class.
In many applications nowadays, it is necessary to rely on other applications during the runtime to guarantee the application integrity. For example, third party applications whose goal is to store a signature from a device installed on the computer. In Java this is pretty easy using the Runtime class, this class allows the application to interface with the environment in which the application is running. For example, in windows you will be able to open the Notepad.exe application using the alias notepad from the CLI, so with Java you should be able to start the notepad.exe application with the following 3 lines of code:
Runtime runTime = Runtime.getRuntime(); String executablePath = "notepad"; Process process = runTime.exec(executablePath);
However, you won’t have always shortcuts for executables, so you will need to provide the absolute path to the executable. In this short article, we’ll provide you with a short snippet that allows you to start a third party application from the system easily.
Full example
The following snippet packed within an application, will start the application (executable) defined in the executablePath variable and will catch any exception triggered by the example:
package sandbox; import java.io.IOException; public class Sandbox < /** * Example of how to run an executable from Java. * * @param args */ public static void main(String[] args) < try < Runtime runTime = Runtime.getRuntime(); String executablePath = "C:\\Users\\sdkca\\AppData\\Local\\Programs\\Microsoft VS Code\\Code.exe"; Process process = runTime.exec(executablePath); >catch (IOException e) < e.printStackTrace(); >> >
If the executable doesn’t exist, the code will catch the exception and will display in the console an output similar to:
java.io.IOException: Cannot run program "my-executable-path.exe": CreateProcess error=2, The system cannot find the file specified at java.lang.ProcessBuilder.start(ProcessBuilder.java:1048) at java.lang.Runtime.exec(Runtime.java:620) at java.lang.Runtime.exec(Runtime.java:450) at java.lang.Runtime.exec(Runtime.java:347) at sandbox.Sandbox.main(Sandbox.java:18) Caused by: java.io.IOException: CreateProcess error=2, The system cannot find the file specified at java.lang.ProcessImpl.create(Native Method) at java.lang.ProcessImpl.(ProcessImpl.java:386) at java.lang.ProcessImpl.start(ProcessImpl.java:137) at java.lang.ProcessBuilder.start(ProcessBuilder.java:1029)