Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Package usage problem

 
Radha MahaLakshmi
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Radha MahaLakshmi
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Radha MahaLakshmi
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ernest
Thanks for the help.
I am able to compile all the 3 java files perfectly.
Thanks and regards
Radha
 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic