Monday, 26 December 2016

Tutorial on Apache Commons Exec API for executing exe files from java program

In my previous post http://java-gui.blogspot.in/2016/12/api-for-executing-exe-files-from-java.html    you have seen using traditional java API to execute exe files from java program.

but it is proved that it is not an ideal solution for  programs which handles IO operations and for async executables. you can find more advantages of Apache Commons Exec library over traditional API in the below link.

              http://commons.apache.org/proper/commons-exec/index.html


Simple example that uses the Exec library:

// class to pass the command ex: cmd.exe and its arguments...

CommandLine command = new CommandLine("ping");

// arguments to the command object can be passed in 2 ways...

command.addArguments("localhost") ;
command.addArguments("-t") ;
command.addArguments("-count");

   or

command.addArguments(new String[]{"-t", "-count"});


//handling the IO operations gracefully using LogOutputStream abstract class


LogOutputStream in = new LogOutputStream() {

@Override
protected void processLine(String line, int exitvalue) {

                       //handle the output which was produced by the process....
                      System.out.println(line);
               }
};
PumpStreamHandler streamHandler = new PumpStreamHandler(in, in);

// Executing the command along with its arguments

DefaultExecutor executor = new DefaultExecutor();

//attaching the created shreamhandler to the executor...

executor.setStreamHandler(streamHandler);

// execution...
executor.execute(command);


the above program executes the ping command and displays the output using the standard out.

Executing the interactive executables can be found in the below link...


link


Tutorial on API for executing exe files from java program.

The traditional API for calling the executable in java program is using the Runtime, Process and their methods like exec(command), waitFor() methods respectively.

a simple example to execute a notepad.exe from java program using the above API is given below.

Process process = Runtime.getRuntime().exec("notepad.exe");

the above code is used to launch the notepad.exe executable from java program.


if there are multiple commands needs to be passed then the code would be....

String[] command = {"cmd.exe", "/C", "echo ", "%TEMP%"}; 
Process process = Runtime.getRuntime().exec(command);

the above snippet is used to execute multiple commands in a java program. this code prints the environment variable TEMP on to the console.


Handling standard output produced by the above code:

BufferedReader input = new BufferedReader(new InputStreamReader(process.getInputStream()));
String output = "";
String line = "";

while((line = input.readLine()) != null){

System.out.println(line);

}

This Procedure is ideal when there is an executable to run without waiting for its completion of execution. despite the a method called p.waitFor() which waits until the process execution is completed, it is difficult to handle the process's standard Outputs and Inputs gracefully. and the no of lines of code also increases drastically.

the alternative to this traditional API is Apache Commons Exec  library.




Thursday, 14 May 2015

Tutorial on JavaFX: arranging components on GridPane

JavaFX provides different layout panes for arranging components on them. for example GridPane, BorderPane etc.
This article describes about arranging components on GridPane. GridPane layout manager allows arranging components/controls using overloaded method. ie add.

                       1. add(controlinstance, colindex, rowindex)

                      EX:  GridPane pane = new GridPane();
                               pane.add(new Separator(), 0, 0);

the above example code puts the separator on 0th row and 0th column of a gridPane.

                     2. add(Node controlinstance,int colIndex,int rowIndex,int colSpan,int rowSpan)   
                          
                     EX: GridPane pane = new GridPane();
                             pane.add(new Separator(),0 , 0, 2, 0);

Following code describes the GridPane with a login screen.


import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.Separator;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.Stage;

import com.atmecs.utilities.Validations;


public class Main extends Application {


Button buttonLogin;
Button buttonCancel;
TextField textFieldUsername;
PasswordField PasswordFieldPassword;


@Override
public void start(Stage primaryStage) {

initComponents(primaryStage);

}

private void initComponents(Stage primaryStage) {
        
GridPane gridPane = new GridPane();

gridPane.setHgap(10);
gridPane.setVgap(10);
gridPane.setPadding(new Insets(25, 25, 25, 25));

Scene scene = new Scene(gridPane, 320, 230);
primaryStage.setScene(scene);

Text title = new Text("Login");
title.setFont(Font.font("Tahoma", FontWeight.BOLD, 20));
gridPane.add(title, 0, 0, 2, 1);

Label labelUsername = new Label("Username:");
gridPane.add(labelUsername, 0, 1);
textFieldUsername = new TextField();
gridPane.add(textFieldUsername, 1, 1);

Label labelPassword = new Label("Password:");
gridPane.add(labelPassword, 0, 2);
PasswordFieldPassword = new PasswordField();
gridPane.add(PasswordFieldPassword, 1, 2);

buttonLogin = new Button("Login");


buttonCancel = new Button("Cancel");

gridPane.add(new Separator(), 0, 4, 2, 1);
HBox hbBtn = new HBox(10);
hbBtn.setAlignment(Pos.BOTTOM_RIGHT);
hbBtn.getChildren().add(buttonLogin);
hbBtn.getChildren().add(buttonCancel);

gridPane.add(hbBtn, 1, 5);

primaryStage.show();
setListeners();
}
        public static void main(String[] args) {
launch(args);
}

}





Output:

Tutorial on Swing Components

 Swing library and it's categorization of components and controls Basically, Swing library provides around 50+ components. These all com...