Results 1 to 5 of 5
  1. #1
    Roam is offline Member
    Join Date
    Jul 2011
    Posts
    3
    Rep Power
    0

    Default Unable to Compile Application

    I'm new to these forums so I just say hi before I make my post.

    I have just installed Java and Notepad++ and here is my application and program codes respectively:

    Java Code:
    public class MyApplication {
    
    	public static void main(String[] args) {
    	
    		MyFirstProgram p = new MyFirstProgram();
    		p.start();
    		
    	}
    	
    }
    and

    Java Code:
    public class MyFirstProgram {
    
    	public static void main(String[] args) {
    	
    		System.out.println("Hello world!");
    		
    	}
    	
    }
    These two files are saved in "C:\MyFirstProgram". First I tried to compile and run the program using command prompt. The program comiled but the application did not:

    Java Code:
    C:\MyFirstProgram>javac MyFirstProgram.java
    
    C:\MyFirstProgram>javac MyApplication.java
    MyApplication.java:6: error: cannot find symbo
                    p.start();
                     ^
      symbol:   method start()
      location: variable p of type MyFirstProgram
    1 error
    I got my codes straight from my textbook so I don't understand what's wrong here. I thought maybe the CLASSPATH is incorrect, so I tried the following which also didn't work:

    Java Code:
    C:\MyFirstProgram>javac -cp . MyFirstProgram.java
    C:\MyFirstProgram>javac -cp . MyApplication.java
    MyApplication.java:6: error: cannot find symbol
                    p.start();
                     ^
      symbol:   method start()
      location: variable p of type MyFirstProgram
    1 error
    So I left command prompt and tried to compile and run the program using Notepad++ but I got the following error:

    Java Code:
    NPP_EXEC: "Java - Run"
    CD: C:\MyFirstProgram
    Current directory: C:\MyFirstProgram
    java MyApplication
    Process started >>>
    java.lang.NoClassDefFoundError: MyApplication
    Caused by: java.lang.ClassNotFoundException: MyApplication
    	at java.net.URLClassLoader$1.run(Unknown Source)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.net.URLClassLoader.findClass(Unknown Source)
    	at java.lang.ClassLoader.loadClass(Unknown Source)
    	at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    	at java.lang.ClassLoader.loadClass(Unknown Source)
    Could not find the main class: MyApplication.  Program will exit.
    Exception in thread "main" <<< Process finished.
    ================ READY ================
    So, what's causing this problem? Any help and guidance is greatly appreciated.
    Last edited by Roam; 07-31-2011 at 07:14 AM.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Hi Roam, welcome to the forums.

    Java Code:
    C:\MyFirstProgram>javac MyApplication.java
    MyApplication.java:6: error: cannot find symbo
                    p.start();
                     ^
      symbol:   method start()
      location: variable p of type MyFirstProgram
    The error is saying that p is of type MyFirstProgram, because that's how you declare it ("MyFirstProgram p = ..."). But the MyFirstProgram class does not define a start() method. You could create a start method and see what happens:

    Java Code:
    public class MyFirstProgram {
    
        public static void main(String[] args) {
    	
            System.out.println("Hello world!");
    		
        }
    
        [color=green]public void start() {
            System.out.println("Hello from the start() method!");
        }[/color]
    }
    -----

    Java Code:
    C:\MyFirstProgram>javac -cp . MyFirstProgram.java
    C:\MyFirstProgram>javac -cp . MyApplication.java
    The value of the CLASSPATH variable is not the problem (if, indeed, it has a value). But what you have there is a good way of compiling the two programs. I say two programs because they both have static void main() methods and they will both run. After compiling both you can see this with:

    Java Code:
    C:\MyFirstProgram>java -cp . MyFirstProgram
    C:\MyFirstProgram>java -cp . MyApplication
    -----

    The Notepad++ error ("java.lang.NoClassDefFoundError: MyApplication") is happening because there is no MyApplication class yet. That's a result of the compile error and will go away once you have MyApplication compiled correctly.

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    I might add that I have no clue what your book was getting at. *Possibly* they meant you to change the MyFirstProgram class so that "public static void main(..." became "public void start(...". Ie

    Java Code:
    public class MyFirstProgram {
    
        [color=green]public static void start()[/color] {
    	
            System.out.println("Hello world!");
    		
        }
    }
    The idea would then be that MyApplication's main() method creates an instance of MyFirstProgram and then calls that instance's start() method to actually run the program.

  4. #4
    chipp's Avatar
    chipp is offline Member
    Join Date
    Jul 2011
    Posts
    62
    Rep Power
    0

    Default

    btw, why it always start with "static" doesn't mean: "contain dynamic value along the program lifetime"? but the start() doesn't it just print line-ing...

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Sorry, in my post #3 the code I suggested you try and think about should not have had the static in it.

    Especially when you start the only thing that should be static is the main() method. "static" means something like "shared by all instances" and is rarely used because most things (methods and variables) are specific to a particular instance. As an example consider the MyApplication class (which is compiling now?):

    Java Code:
    public class MyApplication {
        public static void main(String[] args) {        // 3
            MyFirstProgram p = new MyFirstProgram();    // 1
            p.start();                                  // 2
        }
    }
    (1) When you use "new" you are creating a new instance (of MyFirstProgram)
    (2) The start() method is called with respect to that new instance. Ie start() is not static.
    (3) main() is static because it is not associated with any particular instance of the class.

Similar Threads

  1. Unable to compile...
    By DrakalDragon in forum New To Java
    Replies: 3
    Last Post: 05-14-2011, 12:32 AM
  2. unable to compile class for jsp
    By ashok bhagat in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 11-26-2010, 08:40 AM
  3. Replies: 11
    Last Post: 05-18-2010, 01:49 PM
  4. JasperException: Unable to compile
    By Aerinai in forum Java Servlet
    Replies: 0
    Last Post: 06-17-2008, 06:16 PM
  5. Unable to compile
    By gapper in forum New To Java
    Replies: 2
    Last Post: 01-14-2008, 04:31 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •