• Post Reply Bookmark Topic Watch Topic
  • New Topic

Beginner- Error on Mac 10.10.2  RSS feed

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


I complied this program successfully using - javac helloworld.java using Mac Terminal.
But while running java helloworld.class , it has thrown some exception.

XXXX-MacBook-Pro:hello XXXX$ javac Helloworld.java
XXXX-MacBook-Pro:hello XXXX$ java Helloworld.class
Exception in thread "main" java.lang.NoClassDefFoundError: Helloworld/class
Caused by: java.lang.ClassNotFoundException: Helloworld.class
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)


Please !!help me with this post.
 
Marshal
Posts: 4052
239
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Thanraj, welcome to the Ranch!

When posting code it's best to UseCodeTags (<-- click) to make it nice and easy to read. I've added them for you this time, see how much better it looks.

There are a couple of issues with your code, one relating to your current problem and one that you'll arrive at soon.

Firstly, you need to declare the class public, as in:

Also note that I've capitalised the 'H' in 'Hello', which is a standard convention for writing Java code.

The second issue that you'll come up against now is that the class name and the file name need to be the same. So you'll need to rename the file to Hello.java (note the capitalisation convention again). you should end up with this:

Hello.java

Also note the capitalisation, or lack of, on the argument name 'args'. Again, this is a Java style convention.

See how you get on.
 
Thanraj Danz
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for writing in Tim Cooke. It finally worked. And sure , I ll post my codes using code tags.
 
Ranch Hand
Posts: 417
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hi Tim,

Some corrections follow:

Tim Cooke wrote:Hello Thanraj, welcome to the Ranch!

When posting code it's best to UseCodeTags (<-- click) to make it nice and easy to read. I've added them for you this time, see how much better it looks.

There are a couple of issues with your code, one relating to your current problem and one that you'll arrive at soon.

Firstly, you need to declare the class public, as in:




Not technically true. It runs fine whitout "public".



Also note that I've capitalised the 'H' in 'Hello', which is a standard convention for writing Java code.

The second issue that you'll come up against now is that the class name and the file name need to be the same. So you'll need to rename the file to Hello.java (note the capitalisation convention again). You should end up with this:



All untrue also, it still runs fine without following the recommended convention. You can even name the class Hello and put it in a hello.java file.



Hello.java

Also note the capitalisation, or lack of, on the argument name 'args'. Again, this is a Java style convention.



Also untrue main(String[] Args) runs fine.


See how you get on.


The most important thing in order for this to work is to type:

java Hello

Not:

java Hello.class

It even says so in the posted stack trace.
 
Tim Cooke
Marshal
Posts: 4052
239
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the notes.

By definition 'convention' does not mean 'requirement'.
 
Marshal
Posts: 56605
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A.J. Côté wrote:
. . .
You can even name the class Hello and put it in a hello.java file.
. . .
Not on my machine it didn't. If the class is not public there is no need for any similarity betweeen class name and file name but Tim was specifically writing under the assumption that the class was declared public.
Campbell's Computer wrote:javac hello.java
hello.java:1: error: class Hello is public, should be declared in a file named Hello.java
public class Hello
^
1 error
. . .
Also untrue main(String[] Args) runs fine.
. . .
Tim did say it was a convention.
 
Tim Cooke
Marshal
Posts: 4052
239
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A.J. Côté wrote:Not technically true. It runs fine whitout "public".

True. You are right.

A.J. Côté wrote:All untrue also, it still runs fine without following the recommended convention. You can even name the class Hello and put it in a hello.java file.

True. But only if you have not declared the class public.

A.J. Côté wrote:Also untrue main(String[] Args) runs fine.

True. But it is not good Java writing style and will earn you no friends in industry.
 
A.J. Côté
Ranch Hand
Posts: 417
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Cooke wrote:
True. But it is not good Java writing style and will earn you no friends in industry.


Edit: Sorry, same Tim I guess...

Hi Tim Cooke,

Don't worry, I know the coding conventions and adhere to them pretty well ;-)

In case it was not clearly understood, I guess my point was that the other Tim was quick to jump on coding conventions and code tags without spotting the real problem which was "java Hello", not "java Hello.class" ;-)

Cheers,

 
Tim Cooke
Marshal
Posts: 4052
239
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A.J. Côté wrote:I guess my point was that the other Tim was quick to jump on coding conventions

The other Tim? It's been me all along It's hard to turn off the 'code review' instinct and I'd be remiss to give the impression that functional but poorly formed code is acceptable.
 
A.J. Côté
Ranch Hand
Posts: 417
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Cooke wrote:
A.J. Côté wrote:I guess my point was that the other Tim was quick to jump on coding conventions

The other Tim? It's been me all along It's hard to turn off the 'code review' instinct and I'd be remiss to give the impression that functional but poorly formed code is acceptable.


Yes I did realize that and edited my post before you replied. I got mixed up because Tim Holloway just replied to another post on a thread I posted.

Sorry about that.

Take care!
 
Campbell Ritchie
Marshal
Posts: 56605
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A.J. Côté wrote:. . .
Sorry about that.
. . .
I can get far more confused than that! Apology accepted
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!