Unable to use package...why ???

P

PortisHead

Hello , I'm new to Java and tried the following.

First I created the file : Getbcard.java
inside the directory : C:\myclasses\exam\test\dok
below is the script :
package exam.test.dok;
public class Getbcard
{
public void setname()
{
System.out.println("Hello");
}
}

then , I compiled it using this command line:
javac Getbcard.java
and got the appropriated .class file:
Getbcard.class

Next , I created a new file , named : Showbcard.java in folder : C:
\examples
below is the script :
import exam.test.dok.*;
public class Showbcard
{
public static void main(String[] args)
{
System.out.println("Showbcard");
Getbcard i = new Getbcard();
i.setname();
}
}


then , I compiled it using this command line:
javac -cp c:\myclasses Showbcard.java
and got the appropriated .class file:
Showbcard.class

Now when I try to execute the file like :
java -cp c:\myclasses Showbcard

I get the error : Could not find or load main class Showbcard

What am I doing wrong here ?

Many , many thanks in advance
 
N

Nigel Wade

Hello , I'm new to Java and tried the following.

First I created the file : Getbcard.java
inside the directory : C:\myclasses\exam\test\dok
below is the script :
package exam.test.dok;
public class Getbcard
{
public void setname()
{
System.out.println("Hello");
}
}

then , I compiled it using this command line:
javac Getbcard.java
and got the appropriated .class file:
Getbcard.class

Next , I created a new file , named : Showbcard.java in folder : C:
\examples
below is the script :
import exam.test.dok.*;
public class Showbcard
{
public static void main(String[] args)
{
System.out.println("Showbcard");
Getbcard i = new Getbcard();
i.setname();
}
}


then , I compiled it using this command line:
javac -cp c:\myclasses Showbcard.java
and got the appropriated .class file:
Showbcard.class

Now when I try to execute the file like :
java -cp c:\myclasses Showbcard

I get the error : Could not find or load main class Showbcard

What am I doing wrong here ?

Many , many thanks in advance

You only have the path to Getbcard in your classpath. You also need to
add the path to Showbcard, so java knows where to locate it. From the
limited code you've posted it looks as though Showbcard has no package,
so adding "." (the current directory) to the classpath should suffice.
Normally the default classpath will include ".", but by overriding it
with the -cp argument you have removed ".".

I don't use the java command on Windows directly, so I'm not sure of the
exact syntax of the -cp argument in Windows. In UNIX/Linux it's a colon
separated list of paths, but in Windows-land the colon may interfere
with the ":" as a drive letter separator. Check your local documentation
for the java command, failing that Google should find the answer.
 
P

PortisHead

Hi Nigel

When compiling the Showbcard.java file I included the path where the
package classes reside.
Thus :

javac -cp c:\myclasses Showbcard.java

(and it compiles without any errors , producing a Showbcard.class
file)

Also inside the Showbcard.java file , first line states : import
exam.test.dok.*;

Even when i try to execute it (with java command) , I provide exactly
the class-path,
thus :

java -cp c:\myclasses Showbcard

???
 
N

Nigel Wade

Hi Nigel

When compiling the Showbcard.java file I included the path where the
package classes reside.
Thus :

javac -cp c:\myclasses Showbcard.java

(and it compiles without any errors , producing a Showbcard.class
file)

Also inside the Showbcard.java file , first line states : import
exam.test.dok.*;

Even when i try to execute it (with java command) , I provide exactly
the class-path,
thus :

java -cp c:\myclasses Showbcard

That classpath only contains the location of the Getbcard class. It does
not include the location of Showbcard. Hence java cannot locate the
Showbcard.class file.

javac will look in the current directory for .java files, java will only
do so if the current directory is in the classpath. Your classpath does
not include the current directory (I'm assuming you are in the same
directory as Showbcard.class). You need to include the path to
Showbcard.class in the classpath.
 
M

markspace

That classpath only contains the location of the Getbcard class. It does
not include the location of Showbcard. Hence java cannot locate the
Showbcard.class file.



Specifically, you said you put Showbcard in \examples, not \myclasses.

java -cp c:\myclasses;c:\examples Showbcard

I don't know how your previous claim that you compiled Showbcard with
the class path of c:\myclasses worked. I think there's a mistake in the
sequence of events you showed us.
 
P

PortisHead

The goal to achive is to make Showbcard use Getbcard via the "import"
command.
Thats why Showbcard is in a different directory.
If both classes where in the same directory , I didn't need to make
use of the "package" and "import" techniques.
 
P

PortisHead

Sorry guys...
I tried what markspace told me...and it worked :)

Thus : java -cp c:\myclasses;c:\examples Showbcard
seems to made the difference.

Only thing that I don't understand is...why the above command needs
the "c:\examples" path to work properly ???

The "imported" class is in c:\myclasses , OK , but why does it need
also a reference to the current directory ???
 
P

PortisHead

Sorry Guys.

I tried what markspace told me....and it worked :)

Thus :
java -cp c:\myclasses;c:\examples Showbcard

did the magic.
But I can't understand why , beside "C:\myclasses" where my class
files are stored , it needs the current path of Showbcard ???

Showbcard , imports Getbcard in its scripting.
So it makes pretty much good sense to include the "c:\myclasses" in
the -cp parameter of "java" command,
but why "C:\examples" ???
 
M

markspace

Showbcard , imports Getbcard in its scripting.
So it makes pretty much good sense to include the "c:\myclasses"


I think this is the problem here. "Import" doesn't really import
anything. It just tells the compiler that the class is in use. It's
the java command that does the importing. It needs the directory of
each and every class you use, and it needs the .class files, not the
..java files.

Go look at the location of the .class files you are using, and I think
it'll make sense.

You can actually see the java command load classes if you use the
-verbose option.


java -verbose -cp c:\myclasses;c:\examples Showbcard

<http://download.oracle.com/javase/7/docs/technotes/tools/windows/java.html>
 
M

Martin Gregorie

The "imported" class is in c:\myclasses , OK , but why does it need also
a reference to the current directory ???
The imported class has two rather different uses:

1)the compiler reads it so it can check that what you wrote does actually
match the constructors and classes you're trying to call, but that is
all it does with it. 'import' doesn't mean that the class is imported
into your code. Instead it means that its symbol table is imported into
the javac compiler.

2)the java command creates a JVM containing your class containing the
static void mail(String[]) method and then searches the classpath
to find *and load* the classes your source mentioned in import
statements together with classes they imported and so on, in a cascade
until all these references are satisfied. Then, and only then, can the
program run.

The loader is probably getting classes from a collection of jar files
which are probably not in the same place in the filing system as they
were when the code was compiled. Indeed, its quite likely that the
imported class wasn't even in a jarfile at compile time. All that matters
is that the packagename.ClassName is the same, relative to the runtime
classpath, as the packagename.ClassName was relative to the classpath
that was passed to the compiler.

This is why the compiler class path needs to include "." if packagename
is the name of the directory that contains ClassName.class relative to
the directory where you ran the compiler BUT can still be found when
packagename.ClassName.class has been packaged up in a jar file and put in
some random directory and included in the runtime classpath.
 
M

Martin Gregorie

The "imported" class is in c:\myclasses , OK , but why does it need also
a reference to the current directory ???
The imported class has two rather different uses:

1)the compiler reads it so it can check that what you wrote does actually
match the constructors and classes you're trying to call, but that is
all it does with it. 'import' doesn't mean that the class is imported
into your code. Instead it means that its symbol table is imported into
the javac compiler.

2)the java command creates a JVM containing your class containing the
static void mail(String[]) method and then searches the classpath
to find *and load* the classes your source mentioned in import
statements together with classes they imported and so on, in a cascade
until all these references are satisfied. Then, and only then, can the
program run.

The loader is probably getting classes from a collection of jar files
which are probably not in the same place in the filing system as they
were when the code was compiled. Indeed, its quite likely that the
imported class wasn't even in a jarfile at compile time. All that matters
is that the packagename.ClassName is the same, relative to the runtime
classpath, as the packagename.ClassName was relative to the classpath
that was passed to the compiler.

This is why the compiler class path needs to include "." if packagename
is the name of the directory that contains ClassName.class relative to
the directory where you ran the compiler BUT can still be found when
packagename.ClassName.class has been packaged up in a jar file and put in
some random directory and included in the runtime classpath.
 
M

Martin Gregorie

The imported class has two rather different uses:
.......

Apologies for the double post, but my line went down as I pressed the
button and, after getting the link back online, I thought it hadn't been
sent.
 
L

Lew

PortisHead said:
I tried what markspace told me....and it worked :)

Thus :
java -cp c:\myclasses;c:\examples Showbcard

did the magic.
But I can't understand why , beside "C:\myclasses" where my class
files are stored , it needs the current path of Showbcard ???

How many question marks does it take to indicate an interrogative?
Showbcard , imports Getbcard in its scripting.

There is no "scripting" involved. Java is not a scripting language.
So it makes pretty much good sense to include the "c:\myclasses" in
the -cp parameter of "java" command,
but why "C:\examples" ???

The Java tutorials have a pretty good section on packages and the "import" directive (not command).

Java finds all classes by the classpath. If the classpath does not include the current directory ("."), then the current directory is not searched.
 
N

Nigel Wade

Specifically, you said you put Showbcard in \examples, not \myclasses.

java -cp c:\myclasses;c:\examples Showbcard

I don't know how your previous claim that you compiled Showbcard with
the class path of c:\myclasses worked. I think there's a mistake in the
sequence of events you showed us.

I think the compilation should work because javac locates source files
by normal file paths. It doesn't use the classpath to locate the java
source files, only the supporting class files. So if you specify the
command (as in the OP):
javac -cp c:\myclasses Showbcard.java

it will compile the Showbcard.java source file in the current directory
(no path), and attempt to locate any required classes via the supplied
classpath.

OTOH java only uses class files, and similarly locates them via the
classpath. The supplied classpath in the OP does not include the current
directory (as I've already stated twice) so Showbcard.class cannot be found.
 
R

Roedy Green

java -cp c:\myclasses Showbcard

you have to specify the fully qualified name with package.

See http://mindprod.com/jgloss/package.html
http://mindprod.com/jgloss/javaexe.html

--
Roedy Green Canadian Mind Products
http://mindprod.com
It should not be considered an error when the user starts something
already started or stops something already stopped. This applies
to browsers, services, editors... It is inexcusable to
punish the user by requiring some elaborate sequence to atone,
e.g. open the task editor, find and kill some processes.
 
L

Lew

PortisHead said:
First I created the file : Getbcard.java
inside the directory : C:\myclasses\exam\test\dok
below is the script :
package exam.test.dok;

"myclasses" is the root of the classpath for this class. Directories that match up with packages descend from such a root.
public class Getbcard
{
public void setname()
{
System.out.println("Hello");
}
}

then , I compiled it using this command line:
javac Getbcard.java

From what directory? Where is your "exam/test/dok/" directory path? ("exam\test\dok" in Windows.)
and got the appropriated .class file:
Getbcard.class

In what directory?
Next , I created a new file , named : Showbcard.java in folder : C:
\examples

That sounds like a class with no package. Do not mix classes in packages with classes in the "no-package" (the default package, which isn't really a package).

<http://download.oracle.com/javase/tutorial/java/concepts/package.html>

Packages have to match with directories, in the normal filesystem approach. If they do not, you are fubared.
below is the script :

Not a script.
import exam.test.dok.*;
public class Showbcard
{
public static void main(String[] args)
{
System.out.println("Showbcard");
Getbcard i = new Getbcard();
i.setname();
}
}


then , I compiled it using this command line:
javac -cp c:\myclasses Showbcard.java
and got the appropriated .class file:
Showbcard.class

In what directory?
Now when I try to execute the file like :
java -cp c:\myclasses Showbcard

I get the error : Could not find or load main class Showbcard

What am I doing wrong here ?

As mentioned upthread, you left out the package in the class for the "java" command.

<http://download.oracle.com/javase/tutorial/java/package/index.html>
<http://download.oracle.com/javase/tutorial/java/package/managingfiles.html>

Note: Do not use the CLASSPATH variable. Stick with the "-jar" or "-cp" options to set classpath.

<http://download.oracle.com/javase/7/docs/technotes/tools/index.html>
<http://download.oracle.com/javase/7/docs/technotes/tools/windows/javac.html>
<http://download.oracle.com/javase/7/docs/technotes/tools/windows/java.html>
 
P

PortisHead

"myclasses" is the root of the classpath for this class.  Directories that match up with packages descend from such a root.



From what directory?  Where is your "exam/test/dok/" directory path?  ("exam\test\dok" in Windows.)

From the same directory.That is -----> C:\myclasses\exam\test\dok
In what directory?

In the same as above --->C:\myclasses\exam\test\dok


Next , I created a new file , named : Showbcard.java in folder : C:
\examples

That sounds like a class with no package.  Do not mix classes in packages with classes in the "no-package" (the default package, which isn't really a package).

<http://download.oracle.com/javase/tutorial/java/concepts/package.html>

Packages have to match with directories, in the normal filesystem approach.  If they do not, you are fubared.
below is the script :

Not a script.








import exam.test.dok.*;
public class Showbcard
{
public static void main(String[] args)
{
System.out.println("Showbcard");
Getbcard i = new Getbcard();
i.setname();
}
}
then , I compiled it using this command line:
javac -cp c:\myclasses Showbcard.java
and got the appropriated .class file:
Showbcard.class

In what directory?

the Directory was ----> C:\examples


Thanks again Lew for all the suggestions !!!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,189
Members
46,735
Latest member
HikmatRamazanov

Latest Threads

Top