Re: import statement
varun chadha wrote:
this means if we have a following structure:
otherclasses/
HelloWorld.class
javacode/
Hello.class
otherclasses/
Welcome.class
then as java understands only the namespace(which is package in this
case),
following code is valid:
---Welcome.java---
package otherclasses;
public class Welcome
{...}
---HelloWorld.java---
import Welcome;
public class HelloWorld
{...}
Donkey Hot wrote:
More like this?
---Welcome.java---
package otherclasses.otherclasses;
public class Welcome
{...}
---HelloWorld.java---
package otherclasses;
import otherclasses.otherclasses.Welcome;
public class HelloWorld
{...}
The answer to that question lies in where the classpath is rooted.
varun, you have to show the classpath root element.
I'll give an example in Linux terms. Make appropriate substitutions for
shells in other platforms.
Let us say that you have a directory /opt/projects/. Inside of that you have
a project directory, helloworld/. Thus the project directory is
/opt/projects/helloworld/.
$ cd /opt/projects/helloworld/
Now the current working directory is set at the project directory. Let's
create a source directory src/ and a build directory build/. Just to go
crazy, and to match how NetBeans sets up projects, let's direct all the class
files to build/classes/, so in the end, build/classes/ will be the root of the
classpath.
../ == src/
||
== build/
Let's give the package names something more illuminating than
"otherclasses.otherclasses". Let's create a package 'helloworld'. That goes
directly under the src/ directory. Inside that package we'll put the code for
the class 'HelloWorld'. Under that we'll create a subpackage 'welcome' with
the code for the 'Welcome' class.
../
||= src/
||= helloworld/ = welcome/ = Welcome.java
||= HelloWorld.java
||= build/
||= classes/
||= helloworld/ = welcome/ = Welcome.class
||= HelloWorld.class
In the source:
HelloWorld.java:
=========================
package helloworld;
import helloworld.welcome.Welcome;
public class HelloWorld
{
.... something that uses the 'Welcome' class ...
}
========================
Welcome.java
========================
package helloworld.welcome;
public class Welcome
{
....
}
=======================
Here are the commands for all that (remember, we're parked at the project
directory just above src/ and build/):
$ javac -d build/classes src/testit/HelloWorld.java \
src/testit/welcome/Welcome.java
$ java -cp build/classes testit.HelloWorld
--
Lew