aspose file tools*
The moose likes Beginning Java and the fly likes Getting two java code blocks to work together Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Getting two java code blocks to work together" Watch "Getting two java code blocks to work together" New topic
Author

Getting two java code blocks to work together

Larry Evers
Greenhorn

Joined: Aug 07, 2012
Posts: 14
Hello,
Heres some example code that creates a stack, then a test program that tests that stack. If I set the first one "Stack", in the Netbeans 7 IDE as a Java App project, it builds ok. If I set up the second test program "StackTest" as a separate java project, it will build ok if the "Stack".jar file is added as a library.

But if I try to run the StackTest test program in the IDE, I get a java.lang.NoClassDefFoundError: stacktest/StackTest error. This is my third java program, so any ideas are appreciated. I understand the code here, but I haven't been able to get the "Stack" & "StackTest" portions of it to work together.

// This class defines an integer stack that can hold 10 values.
class Stack {
/* Now, both stck and tos are private. This means
that they cannot be accidentally or maliciously
altered in a way that would be harmful to the stack.
*/
private int stck[] = new int[10];
private int tos;
// Initialize top-of-stack
Stack() {
tos = -1;
}
// Push an item onto the stack
void push(int item) {
if(tos==9)
System.out.println("Stack is full.");
else
stck[++tos] = item;
}
// Pop an item from the stack
int pop() {
if(tos < 0) {
System.out.println("Stack underflow.");
return 0;
}
else
return stck[tos--];
}
}

class TestStack {
public static void main(String args[]) {
Stack mystack1 = new Stack();
Stack mystack2 = new Stack();
// push some numbers onto the stack
for(int i=0; i<10;; i++) mystack1.push(i);
for(int i=10; i<20;; i++) mystack2.push(i);
// pop those numbers off the stack
System.out.println("Stack in mystack1:");
for(int i=0; i<10;; i++)
System.out.println(mystack1.pop());
System.out.println("Stack in mystack2:");
for(int i=0; i<10;; i++)
System.out.println(mystack2.pop());
// these statements are not legal
// mystack1.tos = -2;
// mystack2.stck[3] = 100;
}
}
Kocha Sapam
Ranch Hand

Joined: Aug 04, 2012
Posts: 35
I've a nasty habit of placing all the code in a *.java source code :P

You can do it too if you wish, like this - copy all the code from the Stack.java, paste them just before the class declaration of StackTest in StackTest.java (and i mean just before). You are done, I don't suggest you this though I personaly like this way (it makes your code nasty and hard to maintain)
Larry Evers
Greenhorn

Joined: Aug 07, 2012
Posts: 14
Thanks for the reply,
If I place the Stack code before the StackTest Code in a project called StackTest, the project builds ok, but on running it I get the same error message. Also, in the IDE, the class Stack line of the source code gets an error with the c of class underlined in red, with an error saying "Incorrect Package".

I wonder if theres a problem with the classpath? My projects are in C:\JavaProject, the stacktest project being in C:\JavaProject\stacktest. If I echo the CLASSPATH in a command prompt I get C:\Program Files\Java\jdk1.6.0\bin;C:\Program Files\Java\jre1.6.0\bin;. Maybe I need a reference in the CLASSPATH to C:\JavaProject?


Stuart A. Burkett
Ranch Hand

Joined: May 30, 2012
Posts: 679
Code Lock wrote:... the project builds ok ...


Code Lock wrote:the class Stack line of the source code gets an error with the c of class underlined in red, with an error saying "Incorrect Package".

Those two quotes appear contradictory. If the IDE is showing an error then the code will not compile which could explain why you are getting the NoClassDefFoundError.
The "Incorrect Package" error means your source code file is not in a directory structure that matches your package structure.
Larry Evers
Greenhorn

Joined: Aug 07, 2012
Posts: 14
It is definitely contradictory. The IDE gives me a red underline indicating an error, yet the Build is listed as successfull. Could you expound on your statement:

"your source code file is not in a directory structure that matches your package structure." ?

I suspect some kind of path or directory problem. At the moment I have the Stack and StackTest source code together in the same file called StackTest.java in the directory C:\JavaProject\StackTest.

Stuart A. Burkett
Ranch Hand

Joined: May 30, 2012
Posts: 679
Code Lock wrote:At the moment I have the Stack and StackTest source code together in the same file called StackTest.java in the directory C:\JavaProject\StackTest.

That sounds correct. Do you have a package statement at the top of your source file ? Something like

If you don't then you need one. If you do then does it match the directory name exactly (including case) ?
Larry Evers
Greenhorn

Joined: Aug 07, 2012
Posts: 14
Thanks to Bear for pointing out the site policys in the private messages.

I added a package StackTest; line at the top of the source code containing the Stack and StackTest source code in the C:\JavaProject\StackTest directory where that code resides, and yes I checked that the case was the same for both. Rebuilt the project successfully in Netbeans IDE, but on running it I got the same error and quite a few more, referring to the ClassLoader.

Going to try to catch you guys around 3:30pm as unfortunately I have an appointment. Thanks.
Larry Evers
Greenhorn

Joined: Aug 07, 2012
Posts: 14
Started from Scratch, deleted the C:\JavaProject\Stack and C:\JavaProject\Stacktest directory entries and projects. So all that was left was the C:\JavaProject Directory. Added that to the CLASSPATH. Started a new project in the IDE, only this time I plugged in the code to the template the IDE provides for a new project. The project built successfully and when run, ran ok, pushing & popping numbers to and from the stack and printing them. This was an example program from page 152 of the book "Java: The complete Reference", so the output matched that shown in the book.

All I can figure was that I'd added C:\JavaProject;. to the CLASSPATH, so maybe that was the solution that allowed the program to run. So, just to nail down the solution, I started from scratch again, deleteing the StackTest Project, deleting the C:\JavaProject from the path, and plugging the code into the IDE project template, then rebuilt and ran the StackTest project. And it ran ok.

So, it wasn't a CLASSPATH problem it seems. As I'd noted before my CLASSPATH was C:\program Files\Java\jdk1.6.0\bin;C:\program Files\Java\jre1.6.0\bin;.

I think the problem was me confusing the IDE (and the compiler) with the previous use of two different directories for the Stack and StackTest source code, and, not having the package stacktest; line at the top of the program. Only when I added that, and had started from scratch with the Stack and StackTest source in the same file and a single directory did the program run ok.

So the program now looks like:


Thanks for the replys from a former VB5 user now trying Java. Interesting stuff.
 
wood burning stoves
 
subject: Getting two java code blocks to work together