running java program question


 
Thread Tools Search this Thread
Operating Systems Linux running java program question
# 1  
Old 04-09-2009
running java program question

hi, i have just written a simple hello world java program in my linux server, installed jdk ... and tried to compile and run it and it gave me some errors.

please details below:

Code:
[root@localhost javaFiles]# cat HelloWorld.java
import java.util.*;
import java.io.*;

public class HelloWorld
{
  public static void main(String[] args)
  {
    System.out.println("Hello world!");
  }
}

#javac HelloWorld.java
#
[root@localhost javaFiles]# ls
HelloWorld.class  HelloWorld.java

[root@localhost javaFiles]# java HelloWorld.java
Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorld/java
Caused by: java.lang.ClassNotFoundException: HelloWorld.java
        at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:323)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:268)
        at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:336)

would someone please tell me what is wrong? thanks very much.

Edit: Added code tags. Please use them.

Last edited by Neo; 04-09-2009 at 05:20 PM.. Reason: please use code tags
# 2  
Old 04-09-2009
Looks like you need to set your Java environment variables correctly, for example Jave CLASSPATH.

See this reference:

Classpath (Java) - Wikipedia, the free encyclopedia
# 3  
Old 04-09-2009
thanks, i think that is the problem. do you know which path i should set it to? i know i am supposed the one to know this answer.

i meant, what file (some kind of jar file? ) am i supposed to find in the path that i should set to my java CLASSPATH?

sorry, i am pretty green to the whole thing. i was trying to ask how do i find out which path i should set? i use "yum install jdk* " to download and install my jdk.
# 4  
Old 04-09-2009
You have to set CLASSPATH to the home directory of your Java installation.
# 5  
Old 04-13-2009
Code:
[root@localhost javaFiles]# cat HelloWorld.java
import java.util.*;
import java.io.*;

public class HelloWorld
{
  public static void main(String[] args)
  {
    System.out.println("Hello world!");
  }
}

#javac HelloWorld.java
#
[root@localhost javaFiles]# ls
HelloWorld.class  HelloWorld.java

[root@localhost javaFiles]# java HelloWorld.java
Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorld/java
Caused by: java.lang.ClassNotFoundException: HelloWorld.java
        at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:323)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:268)
        at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:336)

Actually, the problem is having a wrong command, not classpath issue.

It should read

Code:
java HelloWorld

without the extension. The "dot" is considered a namespace separator so Java runtime will look for a file HelloWorld/java.class in the current directory and of course it does not exist.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

java compile and run program help

What is the trick to get a java program to run and compile? I can't even get a basic skeleton to run and compile. What packages do I need? I figured the java-gcj and gcc-java packages was enough. Do I need any other packages? I thought I had to do this and it would work but its not. javac... (1 Reply)
Discussion started by: cokedude
1 Replies

2. Programming

Program java arguments

Hello, The arguments are strings. In my code I need them to be a different type, I do the cast but it is not feasible ... Have you any idea? Thank you (8 Replies)
Discussion started by: chercheur857
8 Replies

3. Programming

Running a Java program on Linux

Hello, I have a project directory in /home/researcher I have in my project/src three packages:A (contains the main class), B and C and I have 2 jar-files file1.jar file2.jar files in the project/my-jars In Class A I import other classes from the other packages I compiled as follows:... (4 Replies)
Discussion started by: chercheur857
4 Replies

4. Programming

need logic for java program

Hi All, I need to to wright a java programm for the following query. Qus: output need to genarate a number+ alphnumric sequence in java and get converted into ASCII code so that i can use mod logic on that. example Output: like 10 digit number 1AAAAAAAAA 1AAAAAAAAB 1AAAAAAAAC . .... (4 Replies)
Discussion started by: Ganesh Khandare
4 Replies

5. Shell Programming and Scripting

Executing a Java Program

I am entirely new to shell scripting and would like to create a script to execute a java program called Main. I've already compiled it and placed the .java and .class files at /root/javaTest. Next I made a shell script that simply contained: java /root/javaTest/Main . I made the script... (2 Replies)
Discussion started by: hypnotic_meat
2 Replies

6. Shell Programming and Scripting

how can i run java program from any where

Hello all sorry didnt know where to post it i compiled simple program that using "System.getProperty("user.dir");" now i compiled the program under directory foo and and its running great when i do "java myapp" i included this directory in the $PATH env variable so i could see it fro any where... (1 Reply)
Discussion started by: umen
1 Replies

7. Shell Programming and Scripting

Invoke java program in script

Hey all, My boss tasked me with the job to write a script which would invoke various java programs, the thing is I don't know much about shell scripting so would you experts help me out? Here is the requirement - 2 applications written in java: App_A and App_B -... (0 Replies)
Discussion started by: mpang_
0 Replies

8. UNIX for Advanced & Expert Users

Invoke java program in different processc d

I have a simple script like this: for file in $dodfiles; do ./rundod $file done $dodfiles is an array of file names selected rundod is a script which invokes a java progrm which process a data file all the processing message logged into a log file in order to extract and analyze the... (1 Reply)
Discussion started by: liux99
1 Replies

9. UNIX for Dummies Questions & Answers

executing the su command from a java program.

Say in unix (AIX) m/c, I am logged in with s1 user and want to start process p1 with user credentials of s2. I can do manually in this way: #su - s2 #enter password for s2> somePassword $ p1 But all this I have to do through a java program. How to pass the password through program. One... (1 Reply)
Discussion started by: shailendrat
1 Replies

10. Programming

Communication between a java and c++ program

Hi, I have the following problem. I have 2 programs, a java program and a c++ program. These 2 programs have to communicate with each other in full duplex mode. I want to use pipes for this communication. So when the c++ program pust something on the stdout the java program must be able to read... (4 Replies)
Discussion started by: lmnt22
4 Replies
Login or Register to Ask a Question