Wednesday Dec 10, 2008

Spring Motion - Physics in JavaFX

3 weeks back, we were thinking of some cool application to make. I am a guy who has seen very less outside world, so coming up with some great idea is always tough for me. So, deciding that, I went back to my tenth class physics book and saw some of the cool physics motion. Its one of the tough subject and always screw me in exam. Searching some of the easy equation, I though to make one spring motion. Meantime, I though there is some spring motion residing in our repository. Actually one of the Josh applications do it in awesome way, but still we were missing the actual feel of Spring motion because of the gig-gag and spiral stuff attached to the wall and spring is going up and down in it, with a complete view of awesomeness :). This is what finally we achieve from this blog :

 I can still bet this can be 3 times much better than what you are seeing here. So, little of good news here that this sample can be executed on mobile

Here are the code files :

1. Main file.

2. Spring file.

3. SpringEquation file.

Enjoy FX'ing !

Friday May 16, 2008

Interaction with Batch File from Java Code

Most of the time when we start server, we write lot of code to optimize the condition. Most of those things are tough to write in Java but easy for batch file or sh file to work on. Like, go into a folder, set JAVA_HOME, setting some heap size and then fire the server. Things are not always start forward, sometime we need to pass some message or some path or some value from Java code to batch file.

Here I tried to write one. Say my JAVA_HOME is dynamic and on some condition I decide what's going to be JAVA_HOME and further on that what java and javac going to run(means setting path). 

import java.io.\*;
public class batchCheck {
    public static void main(String[] args) {
        //All JavaHomes
        String javaHome[] = {"E:\\\\Program Files\\\\Java\\\\j2sdk1.4.2_05", "E:\\\\Program Files\\\\Java\\\\jdk1.6.0"};
        String path;
        String line = "";
        String pathFile = "E:\\\\Program Files\\\\Java\\\\jdk1.6.0\\\\bin\\\\JavaOutput";
        String whichJDK = "";
        String decisionMaker = "142";
        if (decisionMaker.equals("142")) {
            whichJDK = javaHome[0];
        } else {
            whichJDK = javaHome[1];
        }
        try {
            path = whichJDK + "\\\\bin";
            String cmds[] = {"check.bat", pathFile, whichJDK, path};
            Runtime runtime = Runtime.getRuntime();
            Process proc = runtime.exec(cmds);
            proc.getOutputStream().close();
            InputStream inputstream = proc.getInputStream();
            InputStreamReader inputstreamreader = new InputStreamReader(inputstream);
            BufferedReader bufferedreader = new BufferedReader(inputstreamreader);
            while ((line = bufferedreader.readLine()) != null) {
                System.out.println(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Now, you can see my check.bat file:

cd %1
DEL HelloWorld.class
set PATH=%3
set JAVA_HOME=%2
echo %JAVA_HOME%
javac HelloWorld.java
java HelloWorld
exit
 

So, see how we can access the values from java file into batch file from %1, %2, %3 ... ($1,$2... in sh). Simply I moved on to the path where HelloWorld.java resides. I have deleted the old class file and the set the path, set the JAVA_HOME, compiled with javac of new JAVA_HOME and ran the code + exit :D.

Lot many things have been done from a very simple code.

Thursday Mar 13, 2008

How many JRE on my Windows machine

Today we are doing some discussion on JRE and one of my friends Lawrence asked me a question "How to find how many JRE are installed on system by Java Code ? " Now I don't think Java have any such API which will tell how many JRE are installed on System and what are they ? But my another friend Vikram has a saying that JRE installation write information in Registry. And here I tried to write this code. It will only run on Windows :) because again I have used Runtime class. I would love to know how the same could be achieved in Unix Systems. This code is not doing anything, just do query from registry and reflectes the answer on the console. 

import java.io.\*;
class NoofJRE {
static String REG_PATH = "reg query " +
    "\\"HKLM\\\\Software\\\\JavaSoft\\\\Java Runtime Environment";
 
  public static void  getJREInfo() {
    try {
    Process process = Runtime.getRuntime().exec(REG_PATH);
      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 e) {
      System.out.println("I am in Exception");
    }
  }
  public static void main(String s[]) {
    getJREInfo();
    }
}

And here is my output:

E:\\Program Files\\Java\\jdk1.6.0\\bin>java NoofJRE
! REG.EXE VERSION 3.0
HKEY_LOCAL_MACHINE\\Software\\JavaSoft\\Java Runtime Environment
    CurrentVersion      REG_SZ  1.6
    BrowserJavaVersion  REG_SZ  1.6.0_01
HKEY_LOCAL_MACHINE\\Software\\JavaSoft\\Java Runtime Environment\\1.4.2_17
HKEY_LOCAL_MACHINE\\Software\\JavaSoft\\Java Runtime Environment\\1.6
HKEY_LOCAL_MACHINE\\Software\\JavaSoft\\Java Runtime Environment\\1.6.0
HKEY_LOCAL_MACHINE\\Software\\JavaSoft\\Java Runtime Environment\\1.6.0_01

Which sounds true in my case. It is clearly telling me that I have JRE 1.4.2_17, 1.6.0 and 1.6.0_01. I have old bad habit on not uninstalling JRE's :). Please let me know if there is any other way to know how many and which JRE is/are installed in my system.

And to know where it is installed is also easy by querying  JAVA_HOME in registry value.



 

Monday Mar 03, 2008

Listing Java Process from Java

Month Ago, one of my colleagues was making an application, actually the UI flavor of JStack. Since JStack asks you the process ID for attaching the particular Java Application with JStack. In a UI based tool, telling user to do Alt-Ctrl-Del and see the process ID,not sounds good. So, my first impression was that you have to show the java process in the drop down and user will select in that. Some day back even, I want to find all process running on my machine from java code for some stupid purpose. I am trying to write some code for both of them. Java can't play with system process and hence invoking a runtime is only solution to get all process and here it is:


import java.io.\*;
class ListProcess {
public static void main(String[] args)throws IOException
{
          Runtime runtime = Runtime.getRuntime();
           String cmds[] = {"cmd", "/c", "tasklist"};
           Process proc = runtime.exec(cmds);
           InputStream inputstream = proc.getInputStream();
           InputStreamReader inputstreamreader = new InputStreamReader(inputstream);
           BufferedReader bufferedreader = new BufferedReader(inputstreamreader);
           String line;
           while ((line = bufferedreader.readLine()) != null) {
               System.out.println(line);
           }
}
}


Code is written exclusively for Windows Machine :). And one line change in this code will list you only java running process.

String cmds[] = {"cmd", "/c", "jps"}; this is nothing but running jps.exe file in bin (jdk6 onwards). Its not all done. Writting Runtime code is not the real solution as there is little of platform dependencies. So, I have decide to write the code for getting List of Java Process. Again, I have checked by OpenJDK code for jps(search on jps.java file :) ) and I got some hint how to do it and here it goes:

import java.util.\*;
import sun.jvmstat.monitor.\*;

public class ListJavaProcess {
   public static void main(String[] args) throws Exception {

    /\* Checking for local Host, one can do for remote machine as well \*/
          MonitoredHost local = MonitoredHost.getMonitoredHost("localhost");
    /\* Take all active VM's on Host, LocalHost here \*/
     Set vmlist = new HashSet(local.activeVms());
     for (Object id : vmlist) {
       /\* 1234 - Specifies the Java Virtual Machine identified by lvmid 1234 on an unnamed host.
       This string is transformed into the absolute form //1234, which must be resolved against
       a HostIdentifier. \*/
     MonitoredVm vm = local.getMonitoredVm(new VmIdentifier("//" + id));
       /\* take care of class file and jar file both \*/
    String processname = MonitoredVmUtil.mainClass(vm, true);
       System.out.println(id + " ------> " + processname);
     }
   }
}


I have written good amount of comment as it is all together a sun import rather than java or javax import. This import resides in tools.jar, so even running simple javac and java will not work. So, running the program will go here:


E:\\Program Files\\Java\\jdk1.6.0_10\\bin>javac -classpath "E:\\Program Files\\Java\\jd
k1.6.0_10\\lib\\tools.jar" ListJavaProcess.java


E:\\Program Files\\Java\\jdk1.6.0_10\\bin>java -classpath .;"E:\\Program Files\\Java\\j
dk1.6.0_10\\lib\\tools.jar" Vista

3700 ------> ListJavaProcess

Right now only one java process is running. Now in the second code, you can play with some of the java process, but with native process in the above code you can't do anything except watching it :)

No idea how to do this in JDK 1.5 or backwards.



Friday Feb 22, 2008

Not as easy as we thought - PowerShell from Java Runtime

This week, one of my office colleagues,Vijay asked me " Can we run command of PowerShell from Java Application ?" Microsoft is coming up with a new shell called PowerShell, commands are very similar to that of our Unix Shell. Yes, this problem looks quite simple, I guess Java Runtime class will do that as it do for cmd commands. And for that we wrote one simple code like this :

import java.io.\*;
class OneMore {
public static void main(String[] args)throws IOException
{
          Runtime runtime = Runtime.getRuntime();
           String cmds[] = {"ls"};
           Process proc = runtime.exec(cmds);
           InputStream inputstream = proc.getInputStream();
           InputStreamReader inputstreamreader = new InputStreamReader(inputstream);
           BufferedReader bufferedreader = new BufferedReader(inputstreamreader);
           String line;
           while ((line = bufferedreader.readLine()) != null) {
               System.out.println(line);
           }
}
}

So, this code will print the output of ls on console. But no we need to execute powershell command. So it is required to pass PowerShell in the String cmds[]. Ok, I don't have PowerShell on my machine but let me try to run the same with command.exe(cmd.exe) in place of powershell.exe. So, I have changed one line in the code:

String cmds[] = {"cmd", "ls"};

But then this is not right as there should be right option with cmd like /c or /k. So further it changed into :

String cmds[] = {"cmd", "\\c", "ls"};

Code is running fine and we are done with job. But he want a file in place of command "ls", not a problem pass a file "file.bat" in place of ls. file.bat contains ls :). So, finally code goes something like:

Runtime runtime = Runtime.getRuntime();
           String cmds[] = {"cmd", "\\c", "file.bat"};
           Process proc = runtime.exec(cmds);
           InputStream inputstream = proc.getInputStream();
           InputStreamReader inputstreamreader = new InputStreamReader(inputstream);
           BufferedReader bufferedreader = new BufferedReader(inputstreamreader);
           String line;
           while ((line = bufferedreader.readLine()) != null) {
               System.out.println(line);
           }


Even if we want to check the process is exiting correctly or not, we can add some more lines checking for proc.waitFor() or proc.ExitValue().

But, this piece of code is not running fine for PowerShell. WHY ? No clue(till today, trying to find out why). Unfortunately the program go for a hang. And till now the conclusion is: its either a problem that we are not able to find the right substitute of \\c in PowerShell, though the document is saying "&" will do the same Or maybe PowerShell is stopping us in writing streams(some security issue).

A bad workaround is working fine.

Process proc = runtime.exec("powershell & filename" > out.txt)

We are able to pass the output of PowerShell process into a file out.txt, now reading the file out.txt and print the output on console. But it can create problem if out.txt will go large enough. Can you give me the answer of my WHY ? Why this code is not running for PowerShell. Is there any bug in PowerShell(according to me, yes) ?

About

Hi, I am Vaibhav Choudhary working in Sun. This blog is all about simple concept of Java and JavaFX.

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
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
   
       
Today