• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

basic doubt!!

 
ramakrishna pydipati
Greenhorn
Posts: 23
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hai Everybody,
I wrote a program in java and saved in c:\BasicsDemo;
when I tried to compile it is getting compiled but when I tried to run it's not i.e it's giving:
Exception in thread "main" java.lang.NoClassDefFoundError: BasicsDemo
Press any key to continue . . .
I don't know why?
my environmental variables:
JAVA_HOME: C:\Sun\AppServer\jdk;
class path: C:\Sun\AppServer\jdk\bin;
path: C:\Sun\AppServer\jdk\bin
Try to help me ,Thanks in advance.
 
Dan Lastoria
Ranch Hand
Posts: 57
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can we see the source code?
Is the "public class" name also BasicsDemo?
 
ramakrishna pydipati
Greenhorn
Posts: 23
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes name is BasicsDemo.java
import java .io.*;
class BasicsDemo
{
public static void main(String[] args) {
int sum = 0;
for (int current = 1; current <= 10; current++) {
sum += current;
}
System.out.println("Sum = " + sum);
}
}
 
ramakrishna pydipati
Greenhorn
Posts: 23
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got a doubt , is JVM corrupted!!
 
ramakrishna pydipati
Greenhorn
Posts: 23
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
c'mon, I am just trying to solve in by thinking in different ways
 
Layne Lund
Ranch Hand
Posts: 3061
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you please post the commands you use that produce this error message?
 
Layne Lund
Ranch Hand
Posts: 3061
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After taking a closer look at your code, I see that you need to add the word "public" to the class declaration:
public class BasicsDemo
This should fix your problem.
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your CLASSPATH, not your JVM installation, is likely broken. Try adding a . to the CLASSPATH setting, so that the current working directory is included in the search for classes.
You could similarly try running the program with the following.
java -cp . BasicsDemo
 
Ben Wood
Ranch Hand
Posts: 342
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, this one gets me every time when I do a fresh install of the SDK :roll:
Like Dirk says, just add a . to the classpath.
 
sever oon
Ranch Hand
Posts: 268
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First off, you should remove the jdk bin directory from your classpath. If there are any classes in there that you need to run the JDK, they should be relocated to a more appropriate place (my guess is there aren't any there anyway).
The problem you're having is that the JVM is not able to find the compiled class file for BasicsDemo. When you run the JVM, the only thing it knows about is the rt.jar that comes as part of the JDK (that has all the base classes in it, like java.lang.Object and java.lang.String, etc). Everything else it looks into the classpath variable to locate. So, your classpath variable should be "." assuming you always want it to look in the current directory for classes to run.
There is another issue here, and that is one of packages. If you placed your class in the so-called "default package" by omitting a package statement, then you're fine running the java interpreter from the same directory that the class file is located in. Here's an example:

Save this as Foo.java. On my (Windows) machine, I put everything in D:\Users\sever\projects\src, so in the rest of this post assume that's my working directory.
D:\...\src> javac Foo.java
<successful compilation>
D:\...\src> java Foo
foo!
This assumes that my CLASSPATH variable is set to ".". If it was empty, I'd have to type:
D:\...\src> java -classpath . Foo
Now let's say I write a class that I put in some package:

Now, the rules of java dictate that I not only store as a file named Bar.java, but also that the *compiled class file* goes in a directory structure that reflects the packaging. To make clear exactly where to put things, I'm going to be explicit to run it a couple of times from different places:
D:\...\src> javac Bar.java
<success>
D:\...\src> move Bar.class .\example\javaranch
<success>
D:\...\src> java -classpath . example.javaranch.Bar
bar!
See how that works? I could not invoke the interpreter with simply "Bar" anymore, I had to use the package name. Furthermore, the class file had to be put in the example\javaranch location for the interpreter to find it, in addition to the root of the src subtree being added to the classpath. To make this clear, let me execute bar (now in D:\Users\sever\projects\src\example\javaranch) from a different place on disk:
D:\...\src> C:
C:\> java -classpath D:/Users/sever/projects/src example.javaranch.Bar
bar!
Note that I used forward slashes in the classpath argument. This is because Java is OS agnostic, and likes forward slashes to separate directories no matter what platform you're running on. Another thing to add to the pile.
Anyway, hope I cleared some things up for you.
sev
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic