i am quite new to java
i used to code in c++ but now we have to program in java which has not been taught.
i browsed net but casn't get enough information
this is my first code which i managed to get from net
i am using GNU java compiler i.e, gcj in ubuntu

can anyone tell how to get the program completed.
this is what compiler shows
nchy@ubuntu:~/Desktop$ gcj hi.java
hi.java:1: error: The public type HelloWorld must be defined in its own file
public class HelloWorld {
^^^^^^^^^^
1 problem (1 error)
nchy@ubuntu:~/Desktop$ gcj hi.java
hi.java:2: error: The public type HelloWorld must be defined in its own file
public class HelloWorld {
^^^^^^^^^^
1 problem (1 error)

import java.io.*;
public class HelloWorld {

    public static void main(String[] args) {
        System.out.println("Hello, World");
    }

}

It's how java keeps track of what classes are where: Every public class is defined an a source file called classname.java, and the compiled code is in classname.class (where classname is the (case-sensitive) name of the public class).
Sp you need to rename your source file to HelloWorld.java (or rename your class to hi - not recommended!)

can there be any java code without class as in c++.
example

#include<iostream>
using namespace std;
int main()
{
 cout<<"hello world";
}

if so, in that case is there any restriction on filename.

No, there's no java code outside a class definition

can u do me one favour
it would be of great help if u can post whole code which would run.

compiler showa after modification

nchy@ubuntu:~/Desktop$ gcj HelloWorld.java
HelloWorld.java:1: warning: The import java.io is never used
import java.io.*;
^^^^^^^
1 problem (1 warning)
/usr/lib/i386-linux-gnu/gcc/i686-linux-gnu/4.5.2/../../../crt1.o: In function `_start':
(.text+0x18): undefined reference to `main'
collect2: ld returned 1 exit status

The "import java.io is never used" message means what it says - you don't need that import and can simply delete it.
I'm not a Linux buff, so I don;t understand the second half of that output.

But this should definitely run:

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

it is still showing second half of the error i.e, undefined reference to main

Is that coming from the compile, or is it when you try to run the code. Exactly what commands are you using?

I DOWNLOADED GCJ(GNU JAVA COMPILER).
code is same as posted by you.
it is coming from compiler.
this is what it looks like in terminal
nchy@ubuntu:~/Desktop$ gcj HelloWorld.java
/usr/lib/i386-linux-gnu/gcc/i686-linux-gnu/4.5.2/../../../crt1.o: In function `_start':

(.text+0x18): undefined reference to `main'
collect2: ld returned 1 exit status

What was the problem? Please share your solution.
I would always recommend downloading the Oracle "official" JDK rather than any other version.

i was actually using command
gcj HelloWorld.java
instead of
javac HelloWorld.java

is there any other sie better than java tutorials for learning java as cplusplus.com is for c++.

thanks for the advice

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.