| Author |
Why am I not able to this code in eclipse?
|
sagar shiraguppi
Ranch Hand
Joined: Jul 16, 2007
Posts: 74
|
|
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.
|
 |
Hunter McMillen
Ranch Hand
Joined: Mar 13, 2009
Posts: 490
|
|
Try removing the package declaration at the top of your Balance class.
Hunter
|
"If the facts don't fit the theory, get new facts" --Albert Einstein
|
 |
sagar shiraguppi
Ranch Hand
Joined: Jul 16, 2007
Posts: 74
|
|
@Hunter,
I tried after seeing your reply, but still same issue .
|
 |
Hunter McMillen
Ranch Hand
Joined: Mar 13, 2009
Posts: 490
|
|
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
|
 |
sagar shiraguppi
Ranch Hand
Joined: Jul 16, 2007
Posts: 74
|
|
@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.
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32689
|
|
|
What happens when you run the application from the command line?
|
 |
Hunter McMillen
Ranch Hand
Joined: Mar 13, 2009
Posts: 490
|
|
When I try to compile the AccountBalance class I receive this error:
Hunter
|
 |
Hunter McMillen
Ranch Hand
Joined: Mar 13, 2009
Posts: 490
|
|
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
|
 |
sagar shiraguppi
Ranch Hand
Joined: Jul 16, 2007
Posts: 74
|
|
@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
|
 |
Hunter McMillen
Ranch Hand
Joined: Mar 13, 2009
Posts: 490
|
|
The code you posted originally did not have AccountBalance.java in MyPack, are both files in the package ? or just Balance?
Hunter
|
 |
sagar shiraguppi
Ranch Hand
Joined: Jul 16, 2007
Posts: 74
|
|
@Hunter
Eclipse is implicitely created the Mypackage folder also but still i m not able to run
|
 |
Naresh Shanmugam
Ranch Hand
Joined: Jul 16, 2010
Posts: 82
|
|
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
|
 |
 |
|
|
subject: Why am I not able to this code in eclipse?
|
|
|