Results 1 to 7 of 7

Thread: JButton to send command to Java console

  1. #1
    Join Date
    Mar 2010
    Posts
    2,529

    JButton to send command to Java console

    I am not able to execute a system command from my script Java. Yet I have taken the trouble to compare several documentations on the net but none filled me enough to find fault with my code. The simple thing is that I need help for JButton to send command to Java console or anything that can perform the same thing. I am not an expert in Java but started to learn this.

  2. #2
    Join Date
    May 2008
    Posts
    2,297

    Re: JButton to send command to Java console

    Here is the sample code that can be helpful to you for the thing that you need to achieve. Try this code and you will be able to the things.
    Code:
    import java.io. *;
     import javax.swing .*;
     import java.awt.event .*;
     import java.awt.Dimension;
    
     public class extends JFrame (swing2
    		
    	 Public swing2 () 
    	 (
    	
    		 super ("My Window");
    		
    		 / * 
    			  * Management of the closing event of the screen 
    		  *
    		 L = new WindowAdapter WindowListener () (
    			 public void windowClosing (WindowEvent e) (
    				 System. Exit (0);
    			 )
    		 )
    		 addWindowListener (l);
    		
    		 JLabel jLabel1 = new JLabel ("Directory name:"); 
        		 JLabel jLabel2 = new JLabel ("Name *. pdf"); 
    		
    		 JTextField nom_rep = new JTextField ("directory name");
        		 nom_rep. setPreferredSize (new Dimension (150, 20));
        		 JTextField f_pdf = new JTextField ("fichier_pdf");
    		 f_pdf. setPreferredSize (new Dimension (150, 20));
    		
    		 / * 
    			  * Added button 
    		  *
    		 JButton button = new JButton ("create file");
    		
    		 / * 
    			  * Adding a file selector 
    		  *
    		 JFileChooser chooser = new JFileChooser ();
    		
    		 / * 
    			  * Added panel (specify) 
    		  *
    		 JPanel panel = new JPanel ();
    		 panel. add (jLabel1);
    		 panel. add (nom_rep);
    		 panel. add (jLabel2);
    		 / / Panel.add (f_pdf);
    		 panel. add (chooser);
    		 panel. add (button);
    		 button. addActionListener (new click (nom_rep, chooser));
    		
    		 / * 
    			  * Display and dump the buffer panel? 
    		  *
    		 setContentPane (panel);
    		 setSize (800, 100);
    		 setVisible (true);
    	 )
    	
    	 public static void main (String [] args)
    	 (
    		 JFrame frame = new swing2 ();
    	 )
    	
     )
    
     click class implements ActionListener 
     (
    	 JTextField n_rep;
    	 JFileChooser f;
    	 / / String n_rep;
    	 / / String f;
    	 String cmd_create;
    	
    	 click (JTextField c_1, c_2 JFileChooser) 
    	 (
    		 / / System.out.println ("You passed" + c_1 + and + c_2 + "as arguments to the object clicked");
    		 n_rep = c_1;
    		 f = c_2;
    	 )
    	
    	 public void actionPerformed (ActionEvent e) 
    	 (
    		 / / System.out.println ("The button was clicked");
    		 System. Out. System.out.println ("Here are the input values, pos:" + n_rep. GetText () + "pdf and" getSelectedFile + f (). GetName () + "");
    		 cmd_create = "md" + n_rep. getText () + "";
    		 System. Out. System.out.println ("Command:" + cmd_create + "");
    		 Runtime. GetRuntime (). Exec (cmd_create);
    	 )
     )

  3. #3
    Join Date
    Mar 2010
    Posts
    2,529

    Re: JButton to send command to Java console

    Thanks for the quick reply I have tried the code that you have given but the thing is that it is giving error. Is there any problem with the code that you have given, if there is any mistake then please tell me or is there any configuration setting that I need to do for this thing to execute. Whenever I am trying to compile the code it is giving me error message I will be waiting for your reply. I am desperate to know the solution for this problem.

  4. #4
    Join Date
    May 2008
    Posts
    2,297

    Re: JButton to send command to Java console

    Sorry for the mistake that I have done. The exec () method may throw an exception, and it is necessary that you treat that. Just make the following changes in the code.

    public void actionPerformed (ActionEvent e)
    (
    / / System.out.println ("The button was clicked");
    nrep = n_rep. getText ();
    fname = f getSelectedFile (). getName ();
    / / System.out.println ("Here are the values entered, pos:" + n_rep.getText () + 'and pdf: "+ f.getSelectedFile (). GetName () +' ');
    System. Out. System.out.println ("Here are the values entered, pos:" + + nrep "and pdf:" + fname + "");
    cmd_create = "md" nrep + + "";
    System. Out. System.out.println ("Command:" + cmd_create + "");

    try (
    Process proc = Runtime. GetRuntime (). Exec (cmd_create);
    )
    catch (IOException st) (
    System. Out. System.out.println ("Command to create directory not executed"); )
    )

  5. #5
    Join Date
    Mar 2010
    Posts
    2,529

    Re: JButton to send command to Java console

    I have done the change that you have said to do and I have check the information related to the dealing with the exception. It helped me to debug the application in a better way. I wrestled with the result that the error message no longer appears but this does not change the fact that although the windows command "md directory" , I mean this piece of code is also not working. Is there any need to change the security setting to run a system command from Java on Windows XP? Thank you in advance for your answers.

  6. #6
    Join Date
    Feb 2010
    Posts
    644

    Re: JButton to send command to Java console

    Here is the perfect solution for you problem this will explain you why the control system is not running.

    to launch a. Bat
    Code:
    6. / / Cmd = "command.com / cc: \ \ fichier.bat";
        7.                 
        8. / / Win NT (XP. ..): for a start. Bat
        9. / / Cmd = "cmd / cc: \ \ fichier.bat";
       10.  
       11. / / Win 95/98/NT: to launch a. Exe
       12. / / Cmd = "command.com / cc: \ \ windows \ \ notepad.exe";
       13.  
       14. / / Win 95/98/NT: dos command to launch
       15. / / Cmd = "cmd / c copy src.txt dest.txt";
       16.  
       17.  
       18. / / UNIX: To run a script by specifying the shell (sh, bash)
       19. / / Cmd = "/ usr / bin / sh script.sh";
       20.  
       21. / / UNIX: Script to start
       22. / / Cmd = "/ path_complet / tonscript";
    So for those who would not understand if you want the exec () function must return a command like this:

    Code:
      cmd / c md mon_repertoire
    I am sure that this will definitely help you to resolve the issue.

  7. #7
    Join Date
    Feb 2010
    Posts
    538

    Re: JButton to send command to Java console

    Thank you, for the solution that you have provided it has resolved the issue that I was facing. The tip you given is valid for all orders that must be launched from the command window (which makes it a very logical to launch the executable cmd before placing the order). I have tried this for avoiding the manipulation if it is only to create a directory. Since file class is having the method mkdir.

Similar Threads

  1. How to handle JButton Click Event in Java Swing
    By Kungfu Pandey in forum Software Development
    Replies: 3
    Last Post: 20-01-2012, 12:13 PM
  2. Console command for E.Y.E: Divine Cybermancy
    By Egmont in forum Video Games
    Replies: 5
    Last Post: 07-08-2011, 11:35 PM
  3. how to use amx command in console.
    By Xymaya in forum Video Games
    Replies: 4
    Last Post: 05-03-2011, 07:51 AM
  4. How to use Net Send command to Send Message
    By superway in forum Windows Software
    Replies: 1
    Last Post: 15-12-2010, 03:35 PM
  5. JButton does not work in java swing
    By RogerFielden in forum Software Development
    Replies: 3
    Last Post: 15-04-2009, 12:49 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,054,055.30873 seconds with 17 queries