File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes basic doubt!! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "basic doubt!!" Watch "basic doubt!!" New topic
Author

basic doubt!!

ramakrishna pydipati
Greenhorn

Joined: Oct 13, 2003
Posts: 23
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

Joined: Jan 30, 2002
Posts: 57
Can we see the source code?
Is the "public class" name also BasicsDemo?
ramakrishna pydipati
Greenhorn

Joined: Oct 13, 2003
Posts: 23
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

Joined: Oct 13, 2003
Posts: 23
I got a doubt , is JVM corrupted!!
ramakrishna pydipati
Greenhorn

Joined: Oct 13, 2003
Posts: 23
c'mon, I am just trying to solve in by thinking in different ways
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Can you please post the commands you use that produce this error message?


Java API Documentation
The Java Tutorial
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
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

Joined: Dec 10, 2001
Posts: 7023
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


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Ben Wood
Ranch Hand

Joined: Aug 14, 2001
Posts: 342
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.


SCJP 1.4, www.gsi3d.org.uk
sever oon
Ranch Hand

Joined: Feb 08, 2004
Posts: 268
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
 
subject: basic doubt!!
 
Similar Threads
Newbie servlet/j2me question
nOOb Servlet J2ME question
Unable to run web application when moved from Tomcat to Jboss
can't start j2ee server - What to do?
Re: J2EE setting