Closed Thread Icon

Topic awaiting preservation: That newbie stuff you never want to answer because the answer is OBVIOUS (Java) Pages that link to <a href="https://ozoneasylum.com/backlink?for=22372" title="Pages that link to Topic awaiting preservation: That newbie stuff you never want to answer because the answer is OBVIOUS (Java)" rel="nofollow" >Topic awaiting preservation: That newbie stuff you never want to answer because the answer is OBVIOUS (Java)\

 
Author Thread
Alexer
Nervous Wreck (II) Inmate

From: Juneau, Alaska
Insane since: Jun 2004

posted posted 06-30-2004 00:23

I recently wrote my first couple of apps. The first one ran fine. It just delivered two lines of text to the terminal. I made a similar app, but the terminal gave me the following error:

Exception in thread "main" java.lang.NoClassDefFoundError: seanapp/class

Here's the source code:

public class seanapp {

public static void main (String[] args) {
System.out.println("My name is Sean!");
System.out.println("I'm doing Java!");
System.out.println("I have the power TOO!");
}
}


It looks fine to me. I even compared it to my first app. I decided to run the first app again, but got the same error. Here's the source code:

public class MyFirstApp {

public static void main (String[] args) {
System.out.println("I Rule!");
System.out.println("The World");
}
}


What's the deal? This was working before. Thanks in advance.

code-headed intern

WarMage
Maniac (V) Mad Scientist

From: Rochester, New York, USA
Insane since: May 2000

posted posted 06-30-2004 00:57

This error is caused by your file name not matching your class name. You must name your file seanapp.java and you class seanapp. Case is important. It is often the practice to capitalize the first letter of each work so you would have SeanApp.java and public class SeanApp{

Hope that helps.

Dan @ Code Town

Alexer
Nervous Wreck (II) Inmate

From: Juneau, Alaska
Insane since: Jun 2004

posted posted 06-30-2004 01:45

I can see how that would work, but my class/java names already correspond name-wise and case-wize.

Java Name: MyFirstApp.java
Class Name: MyFirstApp.class
Line: public class MyFirstApp {

Java Name: seanapp.java
Class Name: seanapp.class
Line: public class seanapp {

Are there any other possible causes to the error? Thanks

code-headed intern

(Edited by Alexer on 06-30-2004 01:45)

WarMage
Maniac (V) Mad Scientist

From: Rochester, New York, USA
Insane since: May 2000

posted posted 06-30-2004 04:13

Are you trying to run the program by typing "java seanapp.class" because you have to run it by typing "java seanapp" after you have compiled it by typing "javac seanapp.java"

I was able to compile and run your code no problem.

This error is generated when you are trying to run a program and it can not find the coresponding class name in the classpath. Are you doing anything funky with your classpath such as trying to run the program from a directory that the program is not in. You can not run a java program by typing in "java dir/file" you need to run it by typing "java -cp dir file" If this isn't your problem then you might try typing "java -cp . file"

I am on a unix system and haven't run dos in a long time so if the '.' doesn't mean the current directory under windows then make sure the current directory is in your classpath however you do it on your operating system.

Dan @ Code Town

(Edited by WarMage on 06-30-2004 14:53)

Alexer
Nervous Wreck (II) Inmate

From: Juneau, Alaska
Insane since: Jun 2004

posted posted 06-30-2004 07:08

I'm a bit confused.

quote:
Are you trying to run the program by typing "java seanapp.class" because you have to run it by typing "java seanapp" after you have compiled it by typing "javac seanapp.class"



It it's called seanapp.class, then it's already compiled, right? So would "javac seanapp.class" be repetetive, or is this a step I missed? I don't really see how, since, like I said earlier, this app had worked correctly one time. I hadn't made any changes since.

By "classpath," do you mean the directory that the class is in? I am running out of there.

Here's my exact syntax when compiling.. well, almost exact.. Remember, I'm already in the dir that the java is in:




quote:
/scratch/j2sdk1.4.2_04-linux-i586/bin/javac seanapp.java



Thanks for your help thus far.

code-headed intern

norm
Paranoid (IV) Inmate

From: [s]underwater[/s] under-snow in Juneau
Insane since: Sep 2002

posted posted 06-30-2004 07:39

Alexer:

Your app compiled and ran just fine for me! Must be operator error.(lol)

Seriously, I think I know what the problem is. Your program compiled just fine(even though you should be flogged for not capitalizing the class name.), otherwise you would not have gotten .class file. Here is the catch:

To execute the program from the commandline open a terminal, cd into the directory with seanapp.class and use this command- 'java seanapp'

Do not append .class to the file name when using the java command


That one got me a few times in my newbie days too. Oh hold on....I'm still in my Java Newbie days........

One question. Why are you writing Sean's code? Is he bribing you with sodas? You may want to mention that I will gladly code for steak, even a cheap cut.

/*I just have to know...Does Fuzzy Logic tickle?*/

(Edited by norm on 06-30-2004 07:40)

bitdamaged
Maniac (V) Mad Scientist

From: 100101010011 <-- right about here
Insane since: Mar 2000

posted posted 06-30-2004 08:08

class files are compiled Java code. They are what you run. You do understand how Java works right? You compile your code (something.java) into a class file which is java-byte code. That (something.class) is then run by the JVM

javac somefile.class should fail.

Now sometimes your class file depends on other class files to run. So to do so you need to have the files you need in the CLASSPATH environment variable or you need to pass the location of the files to the JVM when you run it ( java -cp that WM mentioned above)

Take a look at the classpath tutes from sun Solaris and Windows

Note the way you set this variable on *nix machines will vary by your shell.



.:[ Never resist a perfect moment ]:.

Alexer
Nervous Wreck (II) Inmate

From: Juneau, Alaska
Insane since: Jun 2004

posted posted 06-30-2004 17:52

Thank you all for your help. My problem wasn't the code or the compilation of said code, but the way I was running the .class. I had been typing "java seanapp.class" when I should have been typing "java seanapp" . Anyway, thanks again for bearing with my newbiness.

code-headed intern

« BackwardsOnwards »

Show Forum Drop Down Menu