LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices

Reply
 
Search this Thread
Old 06-22-2004, 06:38 AM   #1
Lobais
Member
 
Registered: Jan 2004
Location: Denmark
Distribution: Fedora Core 6
Posts: 224

Rep: Reputation: 30
Exception in thread "main" java.lang.NoClassDefFoundError:


OK, I've just got javac working, some sdk stuff, and now I would like java to do the same. I've compiled a small program:
Code:
public class hello
{
        public static void main(String args[])
        {
                System.out.print("Hello world");
        }
}
I have compiled it and renamed the hello.class file for just hello as I saw somewhere on google.
Then when I try to run the program:
Code:
[thomas@thomas Java]$ java hello
Exception in thread "main" java.lang.NoClassDefFoundError: hello
and nothing else happens. What does it mean?
 
Old 06-22-2004, 06:48 AM   #2
Mega Man X
Guru
 
Registered: Apr 2003
Location: ~
Distribution: Ubuntu, FreeBSD, Solaris, DSL
Posts: 5,339

Rep: Reputation: 63
Hi there Lobais!!!

Well, first, you should not rename the file to hello.class. Class is a "compiled" file created by javac. So, write the code as you did, save it as hello.java then use:

javac hello.java

to compile it. java.class will be created. Use:

java hello

to run the application. A side note on your code too. Java can be really peek about System.out line. Substitute you line for this:

System.out.println("Hello world");

That should work. If it fails, reinstall Java2 SDK, it can install wrongly quite often actually

Good luck!
 
Old 06-22-2004, 06:56 AM   #3
Lobais
Member
 
Registered: Jan 2004
Location: Denmark
Distribution: Fedora Core 6
Posts: 224

Original Poster
Rep: Reputation: 30
Well I actually started just with java hello.class, but someone on google wrote that you should remove the .class stuff.
Code:
[thomas@thomas Java]$ javac hello.java
[thomas@thomas Java]$ java hello.class
Exception in thread "main" java.lang.NoClassDefFoundError: hello/class
[thomas@thomas Java]$
I installed Java as the wrote on http://www.fedorafaq.org/custom_java.html just with the sdk package. I think I have installed it more than twice already.
 
Old 06-22-2004, 06:58 AM   #4
Mega Man X
Guru
 
Registered: Apr 2003
Location: ~
Distribution: Ubuntu, FreeBSD, Solaris, DSL
Posts: 5,339

Rep: Reputation: 63
yup, the error is here then:

java hello.class

After you've used "javac hello.java" to compile the application, hello.class will be created. However, to run the application, you've to type "java hello" without the extension (.class)
 
Old 06-22-2004, 07:13 AM   #5
Lobais
Member
 
Registered: Jan 2004
Location: Denmark
Distribution: Fedora Core 6
Posts: 224

Original Poster
Rep: Reputation: 30
That was the thing i didn't understood. Thank you so much.
But why doesn't this work for Anjuta ?

Code:
EXECUTING:
java /home/thomas/Java/hello
----------------------------------------------
Exception in thread "main" java.lang.NoClassDefFoundError: /home/thomas/Java/hello
 
----------------------------------------------
Program exited successfully with errcode (1)
Press the Enter key to close this terminal ...
 
Old 06-22-2004, 07:16 AM   #6
Mega Man X
Guru
 
Registered: Apr 2003
Location: ~
Distribution: Ubuntu, FreeBSD, Solaris, DSL
Posts: 5,339

Rep: Reputation: 63
Hey cool . Well, I've never tried anjuta myself, but everybody says it's great . Sorry that I cannot help ya out with that . However, since we are talking about IDE's, I'd like to recommend DrJava:

http://drjava.sourceforge.net/

It's pretty nice, written in Java, open-source and great for learning Java

Cheers!
 
Old 06-22-2004, 07:21 AM   #7
Lobais
Member
 
Registered: Jan 2004
Location: Denmark
Distribution: Fedora Core 6
Posts: 224

Original Poster
Rep: Reputation: 30
OK, I'll have a look on drjava, but it's a little pretty that I doesn't have OS look
Thanks.
 
Old 06-22-2004, 07:34 AM   #8
aaa
Guru
 
Registered: Jul 2003
Location: VA
Distribution: Slack 10.1
Posts: 2,194

Rep: Reputation: 46
Try 'java -classpath . Hello'
 
Old 06-22-2004, 07:40 AM   #9
Lobais
Member
 
Registered: Jan 2004
Location: Denmark
Distribution: Fedora Core 6
Posts: 224

Original Poster
Rep: Reputation: 30
Instead of the Anjuta "java $(current.full.filename)" execute command?
 
Old 06-22-2004, 08:11 AM   #10
aaa
Guru
 
Registered: Jul 2003
Location: VA
Distribution: Slack 10.1
Posts: 2,194

Rep: Reputation: 46
If you want to use anjuta you will have to do something slightly different.
For 'java -classpath . Hello', make sure you're in the directory that the file's in (w/ cd).
For anjuta, you'd do something like:
java -classpath $(name.of.anjutas.variable.for.the.files.dir) $(current.full.filename)
 
Old 06-22-2004, 08:33 AM   #11
Lobais
Member
 
Registered: Jan 2004
Location: Denmark
Distribution: Fedora Core 6
Posts: 224

Original Poster
Rep: Reputation: 30
I can run the program in the terminal with:
Code:
[thomas@thomas Java]$ javac hello.java
[thomas@thomas Java]$ ls
hello.class  hello.java
[thomas@thomas Java]$ java hello
Hello world
[thomas@thomas Java]$
So no need for classpath there.

In Anjuta, what is the variable.for.the.files.dir thing?
 
Old 06-22-2004, 08:47 AM   #12
Lobais
Member
 
Registered: Jan 2004
Location: Denmark
Distribution: Fedora Core 6
Posts: 224

Original Poster
Rep: Reputation: 30
I made it. I just changed the java "$(current.full.filename)" for java "$(current.file.name)".
But say me one thing. Why can't linux run the class files without using a program? Whit compiled c files, it just runs them.
 
Old 06-22-2004, 09:02 AM   #13
aaa
Guru
 
Registered: Jul 2003
Location: VA
Distribution: Slack 10.1
Posts: 2,194

Rep: Reputation: 46
Java .class files are not executables. They are the code you wrote in a sort of compressed form (it can be easily decompiled, unlike c code). The java program is needed to run them.
 
Old 06-22-2004, 09:07 AM   #14
Lobais
Member
 
Registered: Jan 2004
Location: Denmark
Distribution: Fedora Core 6
Posts: 224

Original Poster
Rep: Reputation: 30
How do I make them into binary?
 
Old 06-22-2004, 06:00 PM   #15
Mega Man X
Guru
 
Registered: Apr 2003
Location: ~
Distribution: Ubuntu, FreeBSD, Solaris, DSL
Posts: 5,339

Rep: Reputation: 63
You "can't", yet. Gcc has an extension that translate java byte code into native code, called gcj. The draw back is, that project is in early stages of development and don't support any graphical interface of java, as Swing.

Also, translating your code into machine language completely kills the purpose of Java, which is portability.

No language is safe, not c/c++ either. There're decompilers for everything. The process of decompiling is known as Reverse Engineering. Take a look here for some decompilers:

http://www.free2space.com/tools/decompilers/setup.html

That said, the two very best ways of protecting your code is: copy right it or release as GPL.

If I was you, I'd more worried to learn Java efficiently (or any other language for that matter) then someone, somehow, in the future, will hack your application. If it does, just sue their pants off. It's quite funny and I bet, profitable
 
  


Reply


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
Starting Java from shell script (Exception in thread "main") rolf_mueller Linux - Software 5 10-30-2004 03:11 AM
Exception in thread "main" java.lang.InternalError: Can't connect to X11 window serve paragvd Programming 4 10-05-2004 04:35 AM
Java error "Exception in thread "main" java.lang.StackOverflowError" nro Programming 1 09-04-2004 04:47 AM
Exception in thread "main" java.lang.NoClassDefFoundError: melinda_sayang Programming 2 04-27-2004 12:49 PM
Exception "NoClassDefFoundError" from java degraffenried13 Linux - Software 8 09-11-2003 12:45 PM


All times are GMT -5. The time now is 04:51 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