Forums Register Login

Why am I not able to this code in eclipse?

+Pie Number of slices to send: Send
Hi friends,

please let me know why I am not able to run this following program. This is program is there in complete reference book (packages);


package MyPack;
class Balance
{
String name;
double bal;

Balance(String n,double b)
{
name=n;
bal=b;
}
void show()
{
if(bal<0)
{
System.out.println("-->");
System.out.println(name+": $"+bal);
}
}
}

class AccountBalance
{
public static void main(String args[])
{
Balance current[]=new Balance[3];

current[0]=new Balance("K.J.Fielding",123.123);
current[1]=new Balance("Will Tell",157.02);
current[2]=new Balance("Tom Jackson",-12.33);

for(int i=0;i<3;i++)
current[i].show();
}
}




I am gettting the following exceptions

1. A java excpetion has occured
2.Could not find the main class,a JNI error has occured, please check installation and try again.

But I m able to run other programs on the same eclipse.

Thanks.
+Pie Number of slices to send: Send
Try removing the package declaration at the top of your Balance class.

Hunter
+Pie Number of slices to send: Send
@Hunter,
I tried after seeing your reply, but still same issue .
+Pie Number of slices to send: Send
Hmm. My compiler was telling me the package didn't exist, so when I removed the package declaration it ran fine. Are the files saved in the same directory?

Hunter
+Pie Number of slices to send: Send
@Hunter
yes,
the directory structre goes like this

src->MyPack-->then the .class files.

I noticed that .class files of both the classes are generated, it means it is compiling fine, but i guess something wrong while running the program.

Please let me know if you have the solution thanks in advance.
+Pie Number of slices to send: Send
What happens when you run the application from the command line?
+Pie Number of slices to send: Send
When I try to compile the AccountBalance class I receive this error:





Hunter
+Pie Number of slices to send: Send
Ok so I feel dumb, I figured out what the issue is. The compiler is looking for a directory called "MyPack" and not finding one. Create a folder called MyPack and add the Balance.java and Balance.class files inside of it. This should fix your issue. Now you just have to import the package in the AccountBalance.java file.

Hunter
+Pie Number of slices to send: Send
@cambell

From command prompt also I am getting this
Exception in thread "main" java.lang.NoClassDefFoundError: MyPack/AccountBala

Caused by: java.lang.ClassNotFoundException: MyPack.AccountBalance
at java.net.URLClassLoader$1.run(URLClassLoader.java:220)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:208)
at java.lang.ClassLoader.loadClass(ClassLoader.java:323)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:268)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:336)
Error: Could not find the main class.
Error: A JNI error has occurred, please check your installation and try again.



I am making sure I am outside Mypack package while running the program and I am using java MyPackage.AccountBalance to run but still no success
+Pie Number of slices to send: Send
The code you posted originally did not have AccountBalance.java in MyPack, are both files in the package ? or just Balance?

Hunter
+Pie Number of slices to send: Send
@Hunter
Eclipse is implicitely created the Mypackage folder also but still i m not able to run:(
+Pie Number of slices to send: Send
Solution for your problem:

Put the below code in "AccountBalance.java"




The below one in "Balance.java"



Compile it like this:

javac -d . AccountBalance.java
javac -d . Balance.java

Run it like this:

java MyPack.AccountBalance
You would be much easier to understand if you took that bucket off of your head. And that goes for the tiny ad too!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 3452 times.
Similar Threads
how to compile packages
package problem
JBuilder 2005 - simple PACKAGE problem - please help me - anybody!!
Simple Package Problem.....
package importing problem
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 04:09:45.