+ -

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}: 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}: 2014 java.lang.Properties  object holds properties information of current runtime environment. Properties class is an implementation of HashTa...

Thursday, 29 May 2014

Print Star( * ) Patterns

Here are some tricks to print * pattern

If you can print pattern like this then you are genius

NOTE : Now all you need to print any other pattern is just satisfy a condition before printing *

Java code to generate above pattern is given below.
save it with name Pattern.java

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

public class Pattern {

 public static void main(String[] args) {
  for (int i = 1; i <= 5; i++) {
   for (int j = 1; j <= 5; j++) {
    System.out.print(" * ");
   }
   System.out.println();
  }
 }

}

In above pattern first loop for (int i = 1; i <= 5; i++)  used for rows
and second loop for (int j = 1; j <= 5; j++) is used for columns

Now if we want to print pattern like this
for this we need to put a condition before printing *

So now we will learn how to make condition to print pattern

Here is explanation for the above pattern 
NOTE : we will make condition based on ( j + i ) and ( j - i )  for any pattern.
Now java code to print the above pattern is


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
package com.trickyjava.pattern;

public class Pattern {

 public static void main(String[] args) {
  for (int i = 1; i <= 5; i++) {
   for (int j = 1; j <= 5; j++) {
    if (i + j <= 6) {             
     System.out.print(" * ");
    } else {                        
     System.out.print("   ");
    }                               
   }
   System.out.println();
  }
 }

}

Here are some other patterns you can try.

Change the condition for the required pattern




Some more patterns

Change no. of rows or columns to print required pattern





5 Tricky {Java}: 2014 Here are some tricks to print * pattern If you can print pattern like this then you are genius NOTE : Now all you need to print any...

Tuesday, 27 May 2014

Stack Implementation without using Array

Here is simple code to implement Stack without using array.

This is Stack.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package com.trickyjava;

public class Stack<T> {
     private StackElement<T> top;

     public T pop() throws Exception {
          if (null == top) {
             throw new Exception("No element in stack");
          }
          T element = top.getElement();
          top = top.getPrevious();
          return element;
     }

     public void push(T element) {
          if (null == top) {
             top = new StackElement<T>(null, element);
             return;
          }
          top = new StackElement<T>(top, element);
     }

     public void empty() {
          top = null;
     }
  
     public boolean hasMoreElements() {
          return null != top;
     }
  
     private class StackElement<T> {
  
          private StackElement<T> previous;
          private T element;
    
          public StackElement(StackElement<T> previous, T element) {
             this.previous = previous;
             this.element = element;
          }
    
          public T getElement() {
             return element;
          }
    
          public StackElement<T> getPrevious() {
             return previous;
          }
    }
}


This is code how you will use it.
This is Application.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
package com.trickyjava;

public class Application {

     public static void main(String[] args) {
        Stack<String> stack = new Stack<>();
        stack.push("This");
        stack.push("is");
        stack.push("stack");
        stack.push("implementation");
        stack.push("without");
        stack.push("using");
        stack.push("array");

        while (stack.hasMoreElements()) {
           try {
              System.out.println(stack.pop());
           } catch (Exception e) {
              e.printStackTrace();
           }
        }
    }

}

This is Output
array
using
without
implementation
stack
is
This
5 Tricky {Java}: 2014 Here is simple code to implement Stack without using array. This is   Stack.java 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 ...

Welcome to www.TrickyJava.com

Hi This is my very first post on www.TrickyJava.com . So basically this blog is for tricky codes in java and java code related problems and solutions. Thanks for support and contribution. 

5 Tricky {Java}: 2014 Hi This is my very first post on www.TrickyJava.com  . So basically this blog is for tricky codes in java and java code related problems a...
<

Followers