• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Why am I not able to this code in eclipse?

 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 492
Firefox Browser VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try removing the package declaration at the top of your Balance class.

Hunter
 
sagar shiraguppi
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Hunter,
I tried after seeing your reply, but still same issue .
 
Hunter McMillen
Ranch Hand
Posts: 492
Firefox Browser VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@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.
 
Marshal
Posts: 79716
381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What happens when you run the application from the command line?
 
Hunter McMillen
Ranch Hand
Posts: 492
Firefox Browser VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When I try to compile the AccountBalance class I receive this error:





Hunter
 
Hunter McMillen
Ranch Hand
Posts: 492
Firefox Browser VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@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
Posts: 492
Firefox Browser VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Hunter
Eclipse is implicitely created the Mypackage folder also but still i m not able to run:(
 
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
I can't beleive you just said that. Now I need to calm down with this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic