Tuesday, March 18, 2014

Get the ProcessList

Below code gives all the processors of your machine.
It will create a ProcessList.txt file for you,with all the processor listed.

package General;

import java.io.*;
import java.util.StringTokenizer;

public class GetProcessList
{

 private String GetProcessListData()
 {
 Process p;
 Runtime runTime;
 String process = null;
 try {
 System.out.println("Processes Reading is started...");

 //Get Runtime environment of System
 runTime = Runtime.getRuntime();

 //Execute command thru Runtime
 p = runTime.exec("tasklist");      // For Windows
 //p=r.exec("ps ux");              //For Linux

 //Create Inputstream for Read Processes
 InputStream inputStream = p.getInputStream();
 InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
 BufferedReader bufferedReader = new BufferedReader(inputStreamReader);

 //Read the processes from sysrtem and add & as delimeter for tokenize the output
 String line = bufferedReader.readLine();
 process = "&";
 while (line != null) {
 line = bufferedReader.readLine();
 process += line + "&";
 }

 //Close the Streams
 bufferedReader.close();
 inputStreamReader.close();
 inputStream.close();

 System.out.println("Processes are read.");
 } catch (IOException e) {
 System.out.println("Exception arise during the read Processes");
 e.printStackTrace();
 }
 return process;
 }

 private void showProcessData()
 {
 try {

 //Call the method For Read the process
 String proc = GetProcessListData();

 //Create Streams for write processes
 //Given the filepath which you need.Its store the file at where your java file.
 OutputStreamWriter outputStreamWriter =
 new OutputStreamWriter(new FileOutputStream("ProcessList.txt"));
 BufferedWriter bufferedWriter = new BufferedWriter(outputStreamWriter);

 //Tokenize the output for write the processes
 StringTokenizer st = new StringTokenizer(proc, "&");

 while (st.hasMoreTokens()) {
 bufferedWriter.write(st.nextToken());  //Write the data in file
 bufferedWriter.newLine();               //Allocate new line for next line
 }

 //Close the outputStreams
 bufferedWriter.close();
 outputStreamWriter.close();

 } catch (IOException ioe) {
 ioe.printStackTrace();
 }

 }

 public static void main(String[] args)
 {
 GetProcessList gpl = new GetProcessList();
 gpl.showProcessData();

 }
}


Starting Windows Services from the java code(Tomcat)

in order to start a particular service you need to confirm that the service is there in the services Tab of the Window Task Manager.Here i will be starting tomcat.

Copy and paste the following code

package General;

import java.io.*;
import java.util.*;

public class Service {
  public static void main(String args[]) {

 String arg1="start";
 String arg2="stop";
    String[] command = {"cmd.exe", "/c", "sc", arg1, "Tomcat7"};
    try {
      Process process = new ProcessBuilder(command).start();
      InputStream inputStream = process.getInputStream();
      InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
      BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
      String line;
      while ((line = bufferedReader.readLine()) != null) {
         System.out.println(line);
      }
    } catch(Exception ex) {
      System.out.println("Exception : "+ex);
    }
  }
}


java-8-streams-map-examples

package com.mkyong.java8; import java.math.BigDecimal; import java.util.ArrayList; import java.util.Arrays; import java.util.List; im...