Disclaimer

The views expressed on this blog are my own and do not necessarily reflect the views of my employer.
Showing posts with label java. Show all posts
Showing posts with label java. Show all posts

Tuesday, May 27, 2008

Playing more with "ejectDrive"

Now start playing with the same program by using the power of netbeans :

At first , click on the 'Runtime' of the following line
'Runtime.getRuntime().exec("eject cdrom");'
Now , you will get a yellow bulb in the left of the editor , same line position. click on it and it will tell you to "Assign Return Value to Variable". click on this line and
'Runtime.getRuntime().exec("eject cdrom");'
will be converted to
'Process exec = Runtime.getRuntime().exec("eject cdrom");'

We will use this exec variable later to perform some another job.

When you executing the command "eject cdrom" in a terminal window , (I am using sxde 9/07) you will get something like :
bash-3.2$ eject cdrom
cdrom /dev/dsk/c0t0d0s2 ejected


Now if you want to print this line : 'cdrom /dev/dsk/c0t0d0s2 ejected' from your program you need to add the following lines in your code :

BufferedReader in = new BufferedReader(new InputStreamReader(exec.getInputStream()));
String line = null;
while ((line = in.readLine()) != null) {
System.out.print(line + "\n");
}


Now use netbeans code completion tool to write programs more easily.
Just write "Bu" and press [CTRL]+[SPACE] and you will get a drop down list of all which starts with 'Bu'. Use arrow cursors to find out BufferReader and press [RETURN].

In the same line when you are writing .....new InputStreamReader(exec.getInputStream()) , wait after writing 'exec.'..... and choose 'getInputStream' from the drop down box.

Now check out that 'BufferReader' and 'InputStreamReader' is underlined by red color which means there's an error in the code . you will also find a red bulb beside. Place your mouse cursor on it to get probable cause of the error and click on it to get possible solution. Just click on it and choose 'Add Import for java.io.BufferReader' etc.

So now everything is complete , indent / format using [CTRL]+[ALT]+[F] and save [CTRL]+[S].

Now 'Main.java' should look like :

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package ejectdrive;

import java.io.BufferedReader;
import java.io.InputStreamReader;

/**
*
* @author ritwik
*/
public class Main {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
try {
Process exec = Runtime.getRuntime().exec("eject cdrom");
BufferedReader in = new BufferedReader(new InputStreamReader(exec.getInputStream()));
String line = null;
while ((line = in.readLine()) != null) {
System.out.print(line + "\n");
}
} catch (Exception e) {
System.out.print(e);
}
}
}


Run your program to get desirable output.

Monday, May 26, 2008

NetBeans IDE 6.1 : simple java application

Don't worry , I won't start with "HelloWorld".. rather say "EjectDrive"..yes, my first program in this IDE was to eject my cd/dvd drive.

It's quite simple by using a terminal window. Just execute a command "eject cdrom" , and your drive will be ejected. Now we will simply try to execute the command from a java program :

1. setup a new project. File -> new project -> java -> java application -> next

2. give a project name , say ejectDrive.

here I have noticed that NB 6.1 provides an opportunity to use a dedicated folder for storing libraries . I will discuss later how it's beneficial to make tasks more simple.

'create main class' & 'set as main project' will be checked automatically , just click 'finish'.

3. Main.java will be opened in the editor window. Here just add the following code under the main method :

try {
Runtime.getRuntime().exec("eject cdrom");
} catch (Exception e) {
System.out.print(e);
}


Now , your Main.java should look like this :

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package ejectdrive;

/**
*
* @author ritwik
*/
public class Main {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
try {
Runtime.getRuntime().exec("eject cdrom");
} catch (Exception e) {
System.out.print(e);
}
}
}



4. Right click -> format or [ALT]+[SHIFT]+[F] to maintain proper indentation.

5. save. [CTRL]+[S]

6. Run. [F6]

and your cddrive will be ejected.

Next : Playing more with "eject drive"