Tuesday, May 29, 2012

Execute Bash script via Java GUI

Couple days ago I got question from My Friend, is there any possibilities to execute bash script with some input from Swing? I didn't answer that question but let me think about it later after got free time.
Today I write answer,
Scenario --> I need to execute bash script with input SUBJECT and EMAIL ADRESS, got 2 input here.

Shell script


just create bash/shell script in your linux/unix machine as per below
#! /bin/sh 

echo "This is content of Email" | /usr/bin/mutt -s "Request by ${1}" ${2}  -a just_test.txt



Tag of ${1} is first input of bash script and ${2} is second input.
I need jSch library to execute bash script, so go to here and download library, then you just include jsch library into your project
This is Swing Gui for execute Bash Script











What I did was , create class with execute bash/shell script with help from jSch library. here my simple code


private void SendEmail(String ntuser, String email) throws Exception {                
        String host = "172.18.129.66";
        String user = "test";
        String pass = "test";
        String command = "/bin/sh /home/cmsuser/testbash.sh "+ntuser+" "+email;
                                    
        try {
            JSch jsch=new JSch(); 
            //jsch.setKnownHosts("/home/cmsuser/.ssh/known_hosts"); 
            Session session = jsch.getSession(user, host);  
            session.setPassword(pass);
            java.util.Properties config=new java.util.Properties();
            config.put("StrictHostKeyChecking", "no");
            session.setConfig(config);
            session.connect(); 
       
            ChannelExec channel = (ChannelExec) session.openChannel("exec"); 
            channel.setCommand(command);  // Pull Report            
            channel.connect(); 
            
            BufferedReader reader = new BufferedReader(new InputStreamReader(channel.getInputStream()));
            String output = reader.readLine();
            while (output != null) {
                System.out.println(output);
                output = reader.readLine();
            }
            reader.close();
            
            channel.disconnect();  
            session.disconnect();  
        
        } catch (Exception t) {
            System.out.println(t);
        }
    }

You must change your host name, user & password before you save and execute your java project. And the next step go to my Github page for more details

Sunday, May 20, 2012

Create Ascii Checker tools with Java Swing

Question : Need ascii check before paste word into CMS Template
Answer : Need fast checker rather than re-type offer one by one.

Create swing project with Netbeans for that purpose.

What important thing is you must decide what ascii encoder you need to use in your checking. my one only use this



static CharsetEncoder asciiEncoder = Charset.forName("US-ASCII").newEncoder(); // or "ISO-8859-1" for ISO Latin 1

  public static boolean isPureAscii(String v) {  
      return asciiEncoder.canEncode(v);
  }


and this is what I build for Ascii Checker

and code for Check button was


private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:
        int koral =0;
        
        for (int j = jTextArea1.getText().length(); j>= koral; j--) {
            String s = jTextArea1.getText().substring(j);//substring(koral, j);            
            if (isPureAscii(s) == false ) {
                jLabel1.setText("isPureAscii() --> "+isPureAscii(jTextArea1.getText())+"  Failed at position : "+ j);
                //System.out.println("Char at position: "+ j);                            
                break;
            }else
                jLabel1.setText("isPureAscii() --> " +isPureAscii(jTextArea1.getText()));
        }
    }           

the next step go to my Github page for more details