Tuesday 20 January 2015

How to run a NetBeans project in command prompt?

You cannot run a file compiled using NetBeans because :
  • while creating new file in netbeans we always first make a new project which automatically creates a  new package
  • the package which is created is automatically added at the top of each of the file made using netbeans eg:
    Package javaapplication3;

    /**
     *
     * @author Rajeev Handa
     */
    public class JavaApplication3 {

        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            // TODO code application logic here
            int i=10;
            System.out.println(i);
        }
     
    }
    In this case the default package made in netbeans is 'javaapplicatiion3'
  • so to run a program in cmd prompt first of all:
  1. Copy the [abc].java file into the bin folder of jdk 
  2. Open the file and edit it with notepad
  3. remove the name of the package i.e. Package javaapplication3
  4. Then press Ctrl+S
  5. Open cmd Prompt
  6. Go to the bin folder or locate it by typing [cd..] command eg:cd java [enter] cd  jdk[enter] cd bin[enter]
  7. Type javac [name_of_file].java
  8. You will see that a class file gets created in bin folder
  9. Then type java [name_of_class] 
  10. Thats it...

No comments:

Post a Comment