+ -

Tuesday, 24 June 2014

Run application using java

You can run application using java by passing command to exec method of java.lang.Runtime object. Here is a sample code for opening calculator in java on Windows or Linux(Ubuntu).

save the below code with name RunApplication.java


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package com.trickyjava.general;

public class RunApplication {

 public static void main(String[] args) {
  try {
   String command = getCalculatorCommand();
   if (null == command) {
    throw new Exception("Command Not found");
   }
   System.out.println("Opening Calculator");
   Runtime runTime = Runtime.getRuntime();
   Process process = runTime.exec(command);
   Thread.sleep(5000);
   System.out.println("Closing Calculator");
   process.destroy();
  } catch (Exception e) {
   e.printStackTrace();
  }
 }

 public static String getCalculatorCommand() {
  String OS = System.getProperty("os.name").toLowerCase();
  if (OS.startsWith("windows")) {
   return "calc";
  } else if (OS.startsWith("linux")) {
   return "gcalctool";
  }
  return null;
 }
}

its method getCalculatorCommand() is used to find-out the Operating Specific command. We have made compatible for Windows and Linux. 

Output of the following code:
Opening Calculator
Closing Calculator

5 Tricky {Java}: June 2014 You can run application using java by passing command to exec method of java.lang.Runtime object. Here is a sample code for opening calcu...

Play with System Properties

java.lang.Properties object holds properties information of current runtime environment. Properties class is an implementation of HashTable, it contains information in the form of Key/Value pairs. System class has five methods to play with system properties.

These methods are :

Getter methods
System.getProperties();
System.getProperty(String key);
System.getProperty(String key, String def);

Setter methods
System.setProperties(Properties properties);
System.setProperty(String key,String value);

To list the system properties save the below code with name ListSystemProperties.java


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
package com.trickyjava.general;

import java.util.Map.Entry;
import java.util.Properties;

public class ListSystemProperties {

 public static void main(String[] args) {
  Properties properties = System.getProperties();
  for (Entry<Object, Object> entry : properties.entrySet()) {
   System.out.println(entry.getKey() + " : " + entry.getValue());
  }
 }
}

And the output of above code will like:

java.runtime.name : OpenJDK Runtime Environment
sun.boot.library.path : /usr/lib/jvm/java-7-openjdk-i386/jre/lib/i386
java.vm.version : 24.51-b03
java.vm.vendor : Oracle Corporation
java.vendor.url : http://java.oracle.com/
path.separator : :
java.vm.name : OpenJDK Server VM
file.encoding.pkg : sun.io
user.country : IN
sun.java.launcher : SUN_STANDARD
sun.os.patch.level : unknown
java.vm.specification.name : Java Virtual Machine Specification
user.dir : /home/trickyJavaAdmin/WORK/WORKSPACE/TrickyJavaCode
java.runtime.version : 1.7.0_55-b14
java.awt.graphicsenv : sun.awt.X11GraphicsEnvironment
java.endorsed.dirs : /usr/lib/jvm/java-7-openjdk-i386/jre/lib/endorsed
os.arch : i386
java.io.tmpdir : /tmp
line.separator : 

java.vm.specification.vendor : Oracle Corporation
os.name : Linux
sun.jnu.encoding : UTF-8
java.library.path : /usr/lib/jvm/java-7-openjdk-i386/jre/lib/i386/server:/usr/lib/jvm/java-7-openjdk-i386/jre/lib/i386:/usr/lib/jvm/java-7-openjdk-i386/jre/../lib/i386:/usr/lib/jvm/java-7-openjdk-i386/jre/lib/i386/client:/usr/lib/jvm/java-7-openjdk-i386/jre/lib/i386::/usr/java/packages/lib/i386:/usr/lib/i386-linux-gnu/jni:/lib/i386-linux-gnu:/usr/lib/i386-linux-gnu:/usr/lib/jni:/lib:/usr/lib
java.specification.name : Java Platform API Specification
java.class.version : 51.0
sun.management.compiler : HotSpot Tiered Compilers
os.version : 3.8.0-34-generic
user.home : /home/trickyJavaAdmin
user.timezone : 
java.awt.printerjob : sun.print.PSPrinterJob
file.encoding : UTF-8
java.specification.version : 1.7
java.class.path : /home/trickyJavaAdmin/WORK/WORKSPACE/TestJava/bin:/home/trickyJavaAdmin/Downloads/google-api-translate-java-0.97.jar
user.name : trickyJavaAdmin
java.vm.specification.version : 1.7
sun.java.command : com.trickyjava.general.ListSystemProperties
java.home : /usr/lib/jvm/java-7-openjdk-i386/jre
sun.arch.data.model : 32
user.language : en
java.specification.vendor : Oracle Corporation
awt.toolkit : sun.awt.X11.XToolkit
java.vm.info : mixed mode
java.version : 1.7.0_55
java.ext.dirs : /usr/lib/jvm/java-7-openjdk-i386/jre/lib/ext:/usr/java/packages/lib/ext
sun.boot.class.path : /usr/lib/jvm/java-7-openjdk-i386/jre/lib/resources.jar:/usr/lib/jvm/java-7-openjdk-i386/jre/lib/rt.jar:/usr/lib/jvm/java-7-openjdk-i386/jre/lib/sunrsasign.jar:/usr/lib/jvm/java-7-openjdk-i386/jre/lib/jsse.jar:/usr/lib/jvm/java-7-openjdk-i386/jre/lib/jce.jar:/usr/lib/jvm/java-7-openjdk-i386/jre/lib/charsets.jar:/usr/lib/jvm/java-7-openjdk-i386/jre/lib/netx.jar:/usr/lib/jvm/java-7-openjdk-i386/jre/lib/plugin.jar:/usr/lib/jvm/java-7-openjdk-i386/jre/lib/rhino.jar:/usr/lib/jvm/java-7-openjdk-i386/jre/lib/jfr.jar:/usr/lib/jvm/java-7-openjdk-i386/jre/classes
java.vendor : Oracle Corporation
file.separator : /
java.vendor.url.bug : http://bugreport.sun.com/bugreport/
sun.io.unicode.encoding : UnicodeLittle
sun.cpu.endian : little
sun.desktop : gnome
sun.cpu.isalist : 

If you want sorted( by property key) list then replace the code main method with the below code

1
2
3
4
5
Properties properties = System.getProperties();
Map<Object, Object> propertiesMap = new TreeMap<>(properties);
for (Entry<Object, Object> entry : propertiesMap.entrySet()) {
 System.out.println(entry.getKey() + " : " + entry.getValue());
}

To set the System properties save the below code with name SetSystemProperties.java


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
package com.trickyjava.general;

public class SetSystemProperties {

 public static void main(String[] args) {
  System.setProperty("movies.home", "/home/trickyJavaAdmin/movies");
  System.setProperty("movies.home.hollywood", "/home/trickyJavaAdmin/movies/hollywoodmovies");
  System.setProperty("movies.home.bollywood", "/home/trickyJavaAdmin/movies/bollywoodmovies");
  
  System.out.println("Movies Directory : "+System.getProperty("movies.home"));
  System.out.println("Hollywood Movies Directory : "+System.getProperty("movies.home.hollywood"));
  System.out.println("Bollywood Movies Directory : "+System.getProperty("movies.home.bollywood"));
 }
}

Output of above code :
Movies Directory : /home/trickyJavaAdmin/movies
Hollywood Movies Directory : /home/trickyJavaAdmin/movies/hollywoodmovies
Bollywood Movies Directory : /home/trickyJavaAdmin/movies/bollywoodmovies

5 Tricky {Java}: June 2014 java.lang.Properties  object holds properties information of current runtime environment. Properties class is an implementation of HashTa...
< >

Followers