tags:

views:

124

answers:

6

How do I create a Java package for different files? I have tried the following. What have I done wrong? And what is the right procedure?

The first file is:

package dil;
public class Hello
{
    Support sp=new Support();
    int i=sp.tin();
    public void man()
    {
       System.out.println(i);
    }
}

The second file is:

package dil;
class Support
{
    public int tin()
    {
        return 3;
    }
}

Now while I compile hello.java it shows these errors:

Hello:4:cannot find symbol
symbol: class Support
location: class dil.hello
Support sp=new Support();
               ^
Hello:4:cannot find symbol
symbol: class Support
location: class dil.hello
Support sp=new Support();
^

Where is the problem and how can I put both these files in a package?

The files are in c:\src.

A: 

I've spotted some things you have to check:

  • class hello starts with a lower case
  • class hello calls for sp.ten() instead of sp.tin()
Alberto Zaccagni
+1  A: 

Although the Support class is not public, that would not be a problem as both classes share the same package. My guess would be that you did not put both source files into a directory according to their packagename and call the javac compiler from the current directory where hello.java resides.

If a class is in package a.b this means the project structure should contain a folder ./a/b containing yourclass.java.

In your case, try to create a folder named ./dil, put your source files in it and call javac from its parent folder.

rsp
Actually I have put both the files in the same directory,and editthe ten() and make it tin(), but still its not working....
Ryan
The crux probably lies in the fact that javac needs a classpath that points to the root package folder. (examle: if your source is in /tmp/dil, run the compiler from /tmp as sourcepath.)
rsp
+1  A: 

See Creating and Using Packages in Sun's Java Tutorials to learn all the details of using packages in Java.

Jesper
+1  A: 

Assuming UNIX / Linux pathnames, a UNIX shell, etc, you need the following file structure:

/some/where/dil
/some/where/dil/hello.java
/some/where/dil/Support.java

Then set $CLASSPATH to /some/where, and compile using the commands

cd /some/where
javac dil/*.java

and run using

java dil.hello

Alternatively, you can tell java and javac what classpath to use with the -cp command line option.

You should also fix the following errors in the code:

  1. Change the name of the "hello" class to "Hello", and rename the source file to match. Strictly speaking this is not an error, but it is a gross violation of the standard for naming Java classes.
  2. You declare a member as "ten" but refer to it as "tin". Fix one or the other.
  3. The entry point method in the "hello" class should be called "main" not "man", and should have a signature public static void main(String[] arg). If you don't fix these the code will compile, but the java command won't find the entry point and will fail.
Stephen C
Still it is giving the same error.
Ryan
The filename should be Hello.java with a capital 'H'.
moxn
Yes ... that's the problem NOW! But if you look at the original version of the question, the class "hello" was in a file called "Hello.java". I give up. Somebody just does not get it ...
Stephen C
A: 

Support isn't public. Make it public and try again.

Martijn Courteaux
It doesn't have to be public as the classes are in the same package.
Carlos
A: 

I suggest you try using one of the free IDEs like Netbeans, Eclipse or IntelliJ CE. This will help you start coding rather than setting everything up the hard way.

BTW: These IDEs have quick fixes for most common problems so they not only give you the error but give you options to fix them and do it for you.

Peter Lawrey