File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Package usage problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Package usage problem" Watch "Package usage problem" New topic
Author

Package usage problem

Radha MahaLakshmi
Ranch Hand

Joined: Mar 28, 2003
Posts: 45
Hi All
I have a problem in using packeges.i have a package like examples.hello
under this package i have 3 classes Hello.java,HelloImpl.java,HelloClient.java.
i compiled Hello.java from c:/examples/hello and it got compiled well.
i tried to compile HelloImpl.java from c:/examples/hello its giving the following error.

hello\HelloImpl.java:8: cannot resolve symbol
symbol : class Hello
location: class examples.hello.HelloImpl
public class HelloImpl extends UnicastRemoteObject implements Hello {
^
1 error
while compiling HelloClient.java also its giving same error.
Please suggest me how to compile all the classes.
i set claspath as c:\example\hello;
also path as c:\example\hello;
i am copyng all the 3 classes below.
Thanks and regard
Radha

class1
------------------
package examples.hello;
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface Hello extends Remote {
String sayHello() throws RemoteException;
}
------------------------
class2
-------------
package examples.hello;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.RMISecurityManager;
import java.rmi.server.UnicastRemoteObject;
public class HelloImpl extends UnicastRemoteObject implements Hello {
public HelloImpl() throws RemoteException {
super();
}
public String sayHello() {
return "Hello World!";
}
public static void main(String args[]) {
// Create and install a security manager
if (System.getSecurityManager() == null) {
System.setSecurityManager(new RMISecurityManager());
}
try {
HelloImpl obj = new HelloImpl();
// Bind this object instance to the name "HelloServer"
Naming.rebind("//myhost/HelloServer", obj);
System.out.println("HelloServer bound in registry");
} catch (Exception e) {
System.out.println("HelloImpl err: " + e.getMessage());
e.printStackTrace();
}
}
}
--------------
class3
---------------
/* * Copyright (c) 2000 BEA Systems, Inc. All Rights Reserved */
package examples.hello;
import java.rmi.RemoteException;
import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;
/** * This class is the sample client for RMI/HelloWorld. * It illustrates JNDI
lookup to find and use a remote object.
* * @author Copyright (c) 2000 by BEA Systems Inc. All Rights Reserved. */
public class HelloClient
{
private static void usage()
{
System.out.println("Usage: java examples.hello.HelloClient corbaloc://<host>:<port>;");
System.exit(1);
}
private static Context getContext(String url) throws NamingException
{
Hashtable env = new Hashtable();
env.put(Context.PROVIDER_URL, url);
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.beasys.jndi.WLEInitialContextFactory");
return new InitialContext(env);
}
public static void main(String[] argv) throws Exception
{
if (argv.length < 1) usage(); String url = argv[0];
Object o = getContext(url).lookup("HelloServer");
Hello obj = (Hello) PortableRemoteObject.narrow(o, Hello.class);
System.out.println(obj.sayHello());
}
}
----------------
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1821

It sounds as thuogh you have a classpath issue. Try the following commands:
cd c:\examples\hello
javac -classpath c:\example\hello Hello.java
javac -classpath c:\example\hello HelloImpl.java
javac -classpath c:\example\hello HelloClient.java
If that works, then either you have made a mistake in setting your system Classpath, or something more sinister is going on. Typically, the former is the case.


Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
Radha MahaLakshmi
Ranch Hand

Joined: Mar 28, 2003
Posts: 45
Hi joel
Thanks for the quick reply.
i tried compiling as you specified.
i am able to compile Hello.java perfectly.
while compiling HelloImpl.java i am getting the same error as below.
C:\examples\hello>javac -classpath c:\examples\hello HelloImpl.java
HelloImpl.java:8: cannot resolve symbol
symbol : class Hello
location: class examples.hello.HelloImpl
public class HelloImpl extends UnicastRemoteObject implements Hello {
^
1 error

Regards
Radha
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Hi,
Welcome to JavaRanch!
This is something that everybody has trouble with when they're learning to program in Java.
The simplest way to look at this problem:
1) The name of a class includes its package name; i.e., the name of class Hello is really examples.hello.Hello .
2) The javac and java tools use CLASSPATH to find classes using their complete names. Javac will not look for a file named Hello.class; it will be looking for examples/hello/Hello.class . Thus the entries on the CLASSPATH are places to look for the examples/ directory, not places to look for the file Hello.class. Get it?
Therefore, if your current directory is examples/hello, and you compile Hello, it will work, because this class doesn't use any of the other classes; but if you try to compile the other two, it will fail, since they refer to the examples.hello.Hello class, and this class can't be found relative to the current directory or to the CLASSPATH.
The correct way to work: don't set CLASSPATH at all; the default class path includes "." (dot) which is a stand-in for the current directory, whatever it happens to be. Have your current directory be c:\. Compile the classes in examples\hello using
javac examples\hello\*.java
and everything will work fine.


[Jess in Action][AskingGoodQuestions]
Radha MahaLakshmi
Ranch Hand

Joined: Mar 28, 2003
Posts: 45
Hi Ernest
Thanks for the help.
I am able to compile all the 3 java files perfectly.
Thanks and regards
Radha
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1821

I must be tired; I usually catch these things. So let me amend:
The amended statement:

Either you have made a mistake in setting your system Classpath, or something more sinister is going on, or I am getting tired. Typically, the former is the case, although the latter has been known to happen.


[ August 14, 2003: Message edited by: Joel McNary ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Package usage problem