LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 10-26-2006, 05:54 AM   #1
mohtasham1983
Member
 
Registered: Apr 2005
Location: San Jose
Distribution: Fedora 3,4- Ubuntu 6.06 to 8.10, Gentoo and Arch
Posts: 408

Rep: Reputation: 30
running java on my Ubuntu 6.06 machine


I've just installed sun-java5-jre and sun-java5-jdk from synaptic and followed instruction in this site: https://help.ubuntu.com/community/Java

After doing all steps I just tried to run a simple Hello World program.
Code:
public class HelloWorld
{
	public static void main(String [] args)
	{
		System.out.println("Hello, World");
	}
}
I did javac HwlloWorld.java and made HelloWorld.class file.
After running "java HelloWorld.class" I got the following error:
Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorld/class

Does anyone know what's wrong with jave on my machine?
 
Old 10-26-2006, 07:27 AM   #2
trevelluk
Member
 
Registered: Nov 2003
Location: Bristol, UK
Distribution: Debian Lenny, Gentoo (at work)
Posts: 388

Rep: Reputation: 32
Nothing's wrong

You should run "java HelloWorld" without the .class extension.

Hope this helps.
 
Old 10-26-2006, 03:53 PM   #3
mohtasham1983
Member
 
Registered: Apr 2005
Location: San Jose
Distribution: Fedora 3,4- Ubuntu 6.06 to 8.10, Gentoo and Arch
Posts: 408

Original Poster
Rep: Reputation: 30
I got the same error after running java HelloWorld.java
 
Old 10-26-2006, 04:06 PM   #4
demon_vox
Member
 
Registered: May 2006
Location: Argentina
Distribution: SuSE 10
Posts: 173

Rep: Reputation: 30
I think that you need to put your HelloWorld.java in a directory which is in the CLASSPATH. You should check what your CLASSPATH variable says.

Cheers!
 
Old 10-26-2006, 04:59 PM   #5
trevelluk
Member
 
Registered: Nov 2003
Location: Bristol, UK
Distribution: Debian Lenny, Gentoo (at work)
Posts: 388

Rep: Reputation: 32
Ah, it's not "java HelloWorld.java" either. Just "java HelloWorld"

demon_vox is right, the file does have to be in a directory in your CLASSPATH, but the current directory (i.e. ".") should always be in this. It can't hurt to check though.
 
Old 10-26-2006, 06:57 PM   #6
mohtasham1983
Member
 
Registered: Apr 2005
Location: San Jose
Distribution: Fedora 3,4- Ubuntu 6.06 to 8.10, Gentoo and Arch
Posts: 408

Original Poster
Rep: Reputation: 30
WOW java HelloWorld works.
Do you know why it is like this? I was told that to run java files one must make a binary file which is readable by java virtual machine and then run the program. But in this case it seems to me I can run this program like an interpreted language.

Anyway thank you very much
 
Old 10-26-2006, 11:09 PM   #7
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,507

Rep: Reputation: 360Reputation: 360Reputation: 360Reputation: 360
Quote:
Originally Posted by mohtasham1983
WOW java HelloWorld works.
Do you know why it is like this? I was told that to run java files one must make a binary file which is readable by java virtual machine and then run the program.
It is indeed the case.
"java HelloWorld" is using HelloWorld.class, which is a binary (bytecode) file.
Quote:
But in this case it seems to me I can run this program like an interpreted language.
It's not the case. Java isn't interpreted.
 
Old 10-27-2006, 07:16 AM   #8
trevelluk
Member
 
Registered: Nov 2003
Location: Bristol, UK
Distribution: Debian Lenny, Gentoo (at work)
Posts: 388

Rep: Reputation: 32
Quote:
Do you know why it is like this?
It's the way Java organises binary files. If you're writing a more complex program, with several classes, you can organise the classes into groups, called packages, to help keep things more organised. A dot is used to separate package and class names, so what's happening when you run "java HelloWorld.class", is Java is looking for a class called "class", in a package called "HelloWorld". Which, of course, doesn't exist. By running "java HelloWorld", then Java will look for a class called "HelloWorld" in the default package, which is what you want.
 
  


Reply

Tags
java


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Java Virtual Machine nodnarb Linux - Software 1 07-19-2004 04:41 PM
Java Virtual machine ServalSoft Linux - Software 1 02-13-2004 08:27 PM
Anyone Know where I can get a Java Virtual Machine? pmorkert Linux - Software 2 07-29-2003 11:17 PM
Java Virtual Machine beajedi Linux - Newbie 4 07-09-2003 01:31 AM
java virtual machine infamous41md Linux - Newbie 6 04-19-2003 11:16 PM


All times are GMT -5. The time now is 06:27 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration