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 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) ?

Tuesday Aug 28, 2007

Bug in Date if written in Hindi

I am watching my last blog and got my eyes on the date. I have set the date to be in Hindi. Now see the last blog date:

रविवार जुलाई ०८, २००७

रविवार is not correct at all. Don't know why is this problem. But it should be

रिववार and I found this a generic mistake like:

शनिवार जून २३, २००७

Ah no, I want my native language to be correct :-). 

 

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