Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

crtvmac

Guest
Original poster
Aug 14, 2009
85
0
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 + ".");
       }
}
 

mrbash

macrumors 6502
Aug 10, 2008
251
1
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.
 

Robb.Penoyer

macrumors member
Mar 8, 2010
69
0
Florida
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.
 

crtvmac

Guest
Original poster
Aug 14, 2009
85
0
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.

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)
 

crtvmac

Guest
Original poster
Aug 14, 2009
85
0

oneeach

macrumors member
Feb 12, 2010
59
0
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)
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.
 

crtvmac

Guest
Original poster
Aug 14, 2009
85
0
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.

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 + ".");
       }
}
 

crtvmac

Guest
Original poster
Aug 14, 2009
85
0
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.

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 + ".");
       }
}
 

mrbash

macrumors 6502
Aug 10, 2008
251
1
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.
 

wrldwzrd89

macrumors G5
Jun 6, 2003
12,110
77
Solon, OH
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.
 

crtvmac

Guest
Original poster
Aug 14, 2009
85
0
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.

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?
 

Attachments

  • ACMLibrary.jpg
    ACMLibrary.jpg
    55.1 KB · Views: 171

crtvmac

Guest
Original poster
Aug 14, 2009
85
0
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.

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'.
 

chown33

Moderator
Staff member
Aug 9, 2009
10,747
8,420
A sea of green
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'.

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.
 

crtvmac

Guest
Original poster
Aug 14, 2009
85
0
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.

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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.