Java Program Wont Compile

Discussion in 'Mac Programming' started by crtvmac, Mar 30, 2010.

  1. crtvmac Guest

    Joined:
    Aug 14, 2009
    #1
    I can't get this Console program to compile so I can run it. Will someone please tell me where my error is?

    HTML:
    import java.io.*;
    
      class Add2Integers extends ConsoleProgram {
          public void run() {
              println("This program adds two numbers.");
              int n1 = readInt("Enter n1: ");
              int n2 = readInt("Enter n2: ");
              int total = n1 + n2;
               println("The total is " +total + ".");
           }
    }
     
  2. Robb.Penoyer macrumors member

    Joined:
    Mar 8, 2010
    Location:
    Florida
  3. mrbash macrumors 6502

    Joined:
    Aug 10, 2008
    #3
    I'm not sure about the class you are extending, but ensure that it actually has the 'println' method. If not, you need to use 'System.out.println' instead.

    Also, you haven't declared the visibility of the class. Generally you would want 'public' visibility.
     
  4. crtvmac thread starter Guest

    Joined:
    Aug 14, 2009
    #4
    The compiler says, "cannot find system". It is referring to the 'class Add2Integers extends ConsoleProgram' line. I am using the BlueJ IDE by the way.
     
  5. Robb.Penoyer macrumors member

    Joined:
    Mar 8, 2010
    Location:
    Florida
    #5
    OK... I know this sounds like a goose chase... but could you post the entire stack trace? It helps set up context for us to help you analyze the problems.
     
  6. crtvmac thread starter Guest

    Joined:
    Aug 14, 2009
    #6
    This is displayed in my Terminal Window application.

    HTML:
    Exception in thread "main" java.lang.NoClassDefFoundError: Add2Integers/java
    Caused by: java.lang.ClassNotFoundException: Add2Integers.java
    	at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
    	at java.lang.ClassLoader.loadClass(ClassLoader.java:315)
    	at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:330)
    	at java.lang.ClassLoader.loadClass(ClassLoader.java:250)
    	at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:398)
    
     
  7. crtvmac thread starter Guest

    Joined:
    Aug 14, 2009
    #7
     
  8. oneeach macrumors member

    Joined:
    Feb 12, 2010
    #8
    There are a few points that I need to make before answering the original question.

    1. This code is based on a package designed to help ease the learning curve of Java. The problem with this approach is that most of us don't use that library (ConsoleProgram is actually acm.program. ConsoleProgram which I have never seen before)

    2. The compiler (javac) is used to convert a .java file into a .class file. This is done before the program executes. I'm guessing you are using an IDE such as Eclipse or NetBeans which compiles automatically for you.

    3. The stack trace is actually from running the program, not compiling it. It does compile, it just doesn't run correctly.

    There are two hints as to what the problem is. The first is the "Exception in thread "main" java.lang.NoClassDefFoundError: Add2Integers/java" message most notably the "Add2Integers/java" on the end. The next is the very next line "Caused by: java.lang.ClassNotFoundException: Add2Integers.java" Remember, in Java the full class name includes the package with periods between the names. You can also put a class inside of a class. For example, if you added "class Foobar" inside of Add2Integers the it would be "Add2Integers.Foobar".

    Now look at the second line of the stack trace. Notice that it says it cannot find "Add2Integers.java"? Somewhere, you told it to run with Add2Integers.java and it is looking for "class java" inside of Add2Integers. To fix it, change the way you start the program with just Add2Integers instead of Add2Integers.java.
     
  9. crtvmac thread starter Guest

    Joined:
    Aug 14, 2009
    #9
    I am just starting to learn Java. Please bear with me. If I wrote the syntax of the program correctly, based on your assistance, I get 'illegal start of expression'. My coding still won't compile using BlueJ IDE.

    HTML:
    import acm.program. *;
    import acm.graphic. *;
        
         class Add2Integer extends consoleprogram {
             public void run() {
             public visibility;
         System.out.println("This program adds two numbers.");
              int n1 = readInt("Enter n1: ");
              int n2 = readInt("Enter n2: ");
              int total = n1 + n2;
              System.out.println("The total is " +total + ".");
           }
    }
     
  10. crtvmac thread starter Guest

    Joined:
    Aug 14, 2009
    #10
    Please bear with me. I am just starting to learn Java. If I added the acm.program class in the correct syntax, my coding still won't compile, using BlueJ IDE. I get 'package acm.program does not exist' when I try to compile.

    HTML:
    import acm.program. *;
    import acm.graphic. *;
        
       class Add2Integer extends graphicsprogram {
              public void run() {
              println("This program adds two numbers.");
              int n1 = readInt("Enter n1: ");
              int n2 = readInt("Enter n2: ");
              int total = n1 + n2;
              System.out.println("The total is " + total + ".");
           }
    }
     
  11. mrbash macrumors 6502

    Joined:
    Aug 10, 2008
    #11
    There are two things I see.

    I am not sure if you are have incorrectly copy/pasted the code, but if you have accurately copied the code in the above message, there are a few things I would fix:

    HTML:
    import acm.program.GraphicsProgram;
        
       public class Add2Integer extends GraphicsProgram {
              public void run() {
              println("This program adds two numbers.");
              int n1 = readInt("Enter n1: ");
              int n2 = readInt("Enter n2: ");
              int total = n1 + n2;
              println("The total is " + total + ".");
           }
    }
    

    The reason the BlueJ IDE is complaining about the package "acm.program" is probably because you haven't added the acm library to your project yet. Locate the "jar" file that is the ACM library and copy it into the BlueJ/lib directory. I'm familiar with the BlueJ IDE but the internet tells me that's where you place your library files.
     
  12. wrldwzrd89 macrumors G5

    wrldwzrd89

    Joined:
    Jun 6, 2003
    Location:
    Solon, OH
    #12
    Also, check the run settings for your program in BlueJ. Do you have it set to launch "Add2Integer.java" instead of "Add2Integer"? That will cause this error, because the Java interpreter thinks you're trying to specify a package name, and cannot find it.
     
  13. crtvmac thread starter Guest

    Joined:
    Aug 14, 2009
    #13
    Its hard to see the path in the attachment where I placed my 'acm.jar' file. It is '/System/Library/Java/Extensions/acm.jar'. The 'acm.jar' file is located in my Extensions directory with a list of other files ending with '.jar'. My coding still won't compile using BlueJ. I get 'Cannot find symbol' and that error message is pretaining to: 'public class Add2Integer extends GraphicsProgram' when I try to compile. Did I put the acm.jar file correctly as you suggest?
     

    Attached Files:

  14. crtvmac thread starter Guest

    Joined:
    Aug 14, 2009
    #14
    Im not sure if I understand the meaning of "Check the run settings for your program in BlueJ." The proceeder I use was, "open project' from Project menu. The project/file that I opened was 'Add2Integers.java'. Then I click the class 'Add2integers' in BlueJ. That brought up my source code. Then I tried to compile that source code and got 'cannot find symbol', which was referring to 'public class Add2Integers extends Consoleprogram'.
     
  15. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #15
    Java is case-sensitive. I've hilited your spelling error in red.


    Earlier:
    Code:
    import acm.program. *;
    import acm.graphic. *;
        
       class Add2Integer extends [COLOR="Red"]graphicsprogram[/COLOR] {
    
    The spelling error is in red again.
     
  16. crtvmac thread starter Guest

    Joined:
    Aug 14, 2009
    #16
    Thanks for the assistance everyone. I finally got my program to run. My last error was in me failing to notice my text was case-sensitive.
     

Share This Page