How to run .exe file by clicking JButton in GUI?

I created a JFrame with 3 Jbuttons. I want the button to run another .exe file located in the same folder. Is it possible? If so, what should I write for actionListener? On the other hand, instead of running an exe file, is it possible to run the main class using JButton? If so, what should I write for actionListener?

Note. The .exe file is created from the main java program.

JButton button1 = new JButton("Program1"); //The JButton name. frame.add(button1); //Add the button to the JFrame. button1.addActionListener().... // how to launch the .exe file 

Thank you in advance

+4
source share
4 answers
 Runtime.getRuntime().exec( ... ); 

or use the ProcessBuilder class.

You should find an example on the Internet that uses these classes.

Edit: for example, to run Execad Exe on Windows you can do:

 Process process = Runtime.getRuntime().exec( "cmd.exe /C start notepad" ); 

If you want to execute a class, you need to call the JVM the same way you called it from the command line.

+4
source

You may be interested in studying the execution of an external process .

This will require Runtime.getRuntime (). exec or new ProcessBuilder

+1
source

I think you would use Runtime.exec () or ProcessBuilder, similar to how you could call an exe program if you did not call it from the GUI. Some things to take care of are that you probably want to call Runtime.exec () in the background thread from the main Swing thread, EDT, for example, provided by the SwingWorker object. Otherwise, your GUI may freeze when the exe program takes a Swing stream. You will also want to head up all the warnings about calling Runtime.exec () shown in this wonderful article, When Runtime.exec () will not be , perhaps one of the best articles written on this topic is highly recommended!

Question: What do you mean by this statement, since it is not clear to me ?:

Note. The program.exe file is created from the main java program.

+1
source

Here is an alternative answer without creating a new process via Runtime.getRuntime (). exec (...) - and you can also maintain your System.in/out channels. However, if you are new to the java programming world and trying to learn the ropes, I would suggest the following camickr advice and not mess with ClassLoader, as described below.

I assume that the class you need to run is self-contained (doesn't use inner classes) and is not yet in your classpath or jarfile, so you can just instantiate and call it main (). If there are multiple class files, simply repeat the method for loading them.

So in ActionListener, your JButton addActionListener () is ed to ...

 public void actionPerformed (ActionEvent e) { String classNameToRun = e.getActionCommand(); // Or however you want to get it try { new MyClassLoader().getInstance(classNameToRun).main (null); } catch (ClassNotFoundException ce) { JOptionPane.showMessageDialog (null, "Sorry, Cannot load class "+classNameToRun, "Your title", JOptionPane.ERROR_MESSAGE); }} 

You will need the new MyClassLoader class already on your class path. Here is the pseudo code:

 import java.io.*; import java.security.*; public class MyClassLoader extends ClassLoader { protected String classDirectory = "dirOfClassFiles" + File.separator, packageName = "packageNameOfClass."; /** * Given a classname, get contents of the class file and return it as a byte array. */ private byte[] getBytes (String className) throws IOException { byte[] classBytes = null; File file = new File (classDirectory + className + ".class"); // Find out length of the file and assign memory // Deal with FileNotFoundException if it is not there long len = file.length(); classBytes = new byte[(int) len]; // Open the file FileInputStream fin = new FileInputStream (file); // Read it into the array; if we don't get all, there an error. // System.out.println ("Reading " + len + " bytes"); int bCount = fin.read (classBytes); if (bCount != len) throw new IOException ("Found "+bCount+" bytes, expecting "+len ); // Don't forget to close the file! fin.close(); // And finally return the file contents as an array return classBytes; } public Class loadClass (String className, boolean resolve) throws IOException, ClassNotFoundException, IllegalAccessException, InstantiationException { Class myClass = findLoadedClass (packageName + className); if (myClass != null) return myClass; byte[] rawBytes = getBytes (className); myClass = defineClass (packageName + className, rawBytes, 0, rawBytes.length); // System.out.println ("Defined class " +packageName + className); if (myClass == null) return myClass; if (resolve) resolveClass (myClass); return myClass; } public Object getInstance (String className) throws ClassNotFoundException { try { return loadClass (className, true).newInstance(); } catch (InstantiationException inExp) { inExp.printStackTrace(); } catch (IllegalAccessException ilExp) { ilExp.printStackTrace(); } catch (IOException ioExp) { ioExp.printStackTrace(); } return null; } } 

Notes: This works well when the class you are trying to load is on your local machine and you start java from the command line. I never managed to get an applet to load a class file from any servlet and load it - security will not allow this. In this case, the workaround is to simply launch another applet in a different window, but this is a different thread. The aforementioned class loading solves the problem of having to split every single file that you might need - just to run the GUI. Good luck, - M.S.

+1
source

Source: https://habr.com/ru/post/1336643/


All Articles