• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Head First Java - probably a really dumb question from the newest newbie ever

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just worked through this first example in the book and I can't figure out why it issues an error when I try to run it.

public class MyFirstApp {

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

}


I saved it as MyFirstApp.java. I compiled it (javac MyFirstApp.java) and it generated MyFirstApp.class. I typed at the command line "java MyFirstApp.class" and it says:

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

So what gives? Any help is gratiously appreciated.
[ December 18, 2005: Message edited by: zach sycks ]
 
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I saved it as MyFirstApp.java. I compiled it (javac MyFirstApp.java) and it generated MyFirstApp.class. I typed at the command line "java MyFirstApp.class" and it says:



U have to give "java MyFirstApp" without that .class.

And Hey u can look at
this url for more info on running a Java Prog.
[ December 18, 2005: Message edited by: Sri Ram ]
 
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Zach,

Inorder that your program should execute just type the following command,
java MyFirstApp
You dont need the dot class extension.
Have fun
[ December 18, 2005: Message edited by: Abdulla Mamuwala ]
 
zach sycks
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
just typed that at the command prompt and got the same error. argh.
 
Abdulla Mamuwala
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is your class file in the same directory from where you are executing your java command. That is if you have your .class file in say C:\MyPrograms, change directory to that particular directory and than execute your java command i.e, C:\MyPrograms\java MyFirstApp
Good Luck
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by zach sycks:
just typed that at the command prompt and got the same error. argh.


Are you getting exactly the same error? Or is the the same error without "/class" at the end?

See this thread.
[ December 18, 2005: Message edited by: marc weber ]
 
zach sycks
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Checking out that thread, looks like the right thing so far! It is indeed the same error, but without /class as you've pointed out.

Thanks!
 
zach sycks
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Per the other thread, I typed:

java -cp . MyFirstApp and that took care of it!

Also, if I read the prior thread correctly, I needed to put a . in my CLASSPATH for Windows XP. I did that as well, we'll see what happens.
 
I will suppress my every urge. But not this shameless plug:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic