Disclaimer

The views expressed on this blog are my own and do not necessarily reflect the views of Oracle.

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"

2 comments:

  1. Hello, I tried running your program on NetBeans IDE6.5.1 but I keep on getting this run time error: run:
    java.io.IOException: Cannot run program "eject": CreateProcess error=2, The system cannot find the file specifiedBUILD SUCCESSFUL (total time: 0 seconds)

    Do you know why? Sorry that I'm a beginner at this..

    ReplyDelete
  2. http://forums.sun.com/thread.jspa?threadID=5302579

    Use this and it works!!

    ReplyDelete