Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #9165 > unrolled thread
| Started by | PortisHead <massivetdm850@gmail.com> |
|---|---|
| First post | 2011-10-25 00:06 -0700 |
| Last post | 2011-10-28 12:57 -0700 |
| Articles | 18 — 6 participants |
Back to article view | Back to comp.lang.java.programmer
Unable to use package...why ??? PortisHead <massivetdm850@gmail.com> - 2011-10-25 00:06 -0700
Re: Unable to use package...why ??? Nigel Wade <nmw-news@ion.le.ac.uk> - 2011-10-25 10:32 +0100
Re: Unable to use package...why ??? PortisHead <massivetdm850@gmail.com> - 2011-10-25 04:10 -0700
Re: Unable to use package...why ??? Nigel Wade <nmw-news@ion.le.ac.uk> - 2011-10-25 13:07 +0100
Re: Unable to use package...why ??? markspace <-@.> - 2011-10-25 07:47 -0700
Re: Unable to use package...why ??? PortisHead <massivetdm850@gmail.com> - 2011-10-25 15:03 -0700
Re: Unable to use package...why ??? PortisHead <massivetdm850@gmail.com> - 2011-10-25 15:10 -0700
Re: Unable to use package...why ??? Martin Gregorie <martin@address-in-sig.invalid> - 2011-10-26 00:06 +0000
Re: Unable to use package...why ??? Martin Gregorie <martin@address-in-sig.invalid> - 2011-10-26 00:08 +0000
Re: Unable to use package...why ??? Martin Gregorie <martin@address-in-sig.invalid> - 2011-10-26 00:06 +0000
Re: Unable to use package...why ??? PortisHead <massivetdm850@gmail.com> - 2011-10-25 15:21 -0700
Re: Unable to use package...why ??? markspace <-@.> - 2011-10-25 15:57 -0700
Re: Unable to use package...why ??? Lew <lewbloch@gmail.com> - 2011-10-25 20:05 -0700
Re: Unable to use package...why ??? Nigel Wade <nmw-news@ion.le.ac.uk> - 2011-10-26 09:55 +0100
Re: Unable to use package...why ??? PortisHead <massivetdm850@gmail.com> - 2011-10-26 13:08 -0700
Re: Unable to use package...why ??? Roedy Green <see_website@mindprod.com.invalid> - 2011-10-27 07:20 -0700
Re: Unable to use package...why ??? Lew <lewbloch@gmail.com> - 2011-10-27 07:33 -0700
Re: Unable to use package...why ??? PortisHead <massivetdm850@gmail.com> - 2011-10-28 12:57 -0700
| From | PortisHead <massivetdm850@gmail.com> |
|---|---|
| Date | 2011-10-25 00:06 -0700 |
| Subject | Unable to use package...why ??? |
| Message-ID | <280344ac-b398-44b3-b775-e84859feb5f0@gy7g2000vbb.googlegroups.com> |
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
[toc] | [next] | [standalone]
| From | Nigel Wade <nmw-news@ion.le.ac.uk> |
|---|---|
| Date | 2011-10-25 10:32 +0100 |
| Message-ID | <9gne1vFvskU1@mid.individual.net> |
| In reply to | #9165 |
On 25/10/11 08:06, PortisHead wrote:
> 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.
--
Nigel Wade
[toc] | [prev] | [next] | [standalone]
| From | PortisHead <massivetdm850@gmail.com> |
|---|---|
| Date | 2011-10-25 04:10 -0700 |
| Message-ID | <02de641c-1c6a-420b-b059-c17444e1ff3e@g25g2000yqh.googlegroups.com> |
| In reply to | #9167 |
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 ???
[toc] | [prev] | [next] | [standalone]
| From | Nigel Wade <nmw-news@ion.le.ac.uk> |
|---|---|
| Date | 2011-10-25 13:07 +0100 |
| Message-ID | <9gnn4cFbaqU1@mid.individual.net> |
| In reply to | #9171 |
On 25/10/11 12:10, PortisHead wrote: > 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. -- Nigel Wade
[toc] | [prev] | [next] | [standalone]
| From | markspace <-@.> |
|---|---|
| Date | 2011-10-25 07:47 -0700 |
| Message-ID | <j86i64$j5u$1@dont-email.me> |
| In reply to | #9172 |
On 10/25/2011 5:07 AM, Nigel Wade wrote: > On 25/10/11 12:10, PortisHead wrote: >> >> 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. 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.
[toc] | [prev] | [next] | [standalone]
| From | PortisHead <massivetdm850@gmail.com> |
|---|---|
| Date | 2011-10-25 15:03 -0700 |
| Message-ID | <f756ecfd-0c89-4ab0-abb8-d94be8ac2361@o19g2000vbk.googlegroups.com> |
| In reply to | #9173 |
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.
[toc] | [prev] | [next] | [standalone]
| From | PortisHead <massivetdm850@gmail.com> |
|---|---|
| Date | 2011-10-25 15:10 -0700 |
| Message-ID | <2482c1e3-adde-4a2f-a245-a129958b4ba6@n13g2000vbv.googlegroups.com> |
| In reply to | #9179 |
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 ???
[toc] | [prev] | [next] | [standalone]
| From | Martin Gregorie <martin@address-in-sig.invalid> |
|---|---|
| Date | 2011-10-26 00:06 +0000 |
| Message-ID | <j87it8$50i$1@localhost.localdomain> |
| In reply to | #9180 |
On Tue, 25 Oct 2011 15:10:43 -0700, PortisHead wrote: > 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. -- martin@ | Martin Gregorie gregorie. | Essex, UK org |
[toc] | [prev] | [next] | [standalone]
| From | Martin Gregorie <martin@address-in-sig.invalid> |
|---|---|
| Date | 2011-10-26 00:08 +0000 |
| Message-ID | <j87j2c$50o$1@localhost.localdomain> |
| In reply to | #9185 |
On Wed, 26 Oct 2011 00:06:01 +0000, Martin Gregorie wrote: > On Tue, 25 Oct 2011 15:10:43 -0700, PortisHead wrote: > >> 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: > ...... 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. -- martin@ | Martin Gregorie gregorie. | Essex, UK org |
[toc] | [prev] | [next] | [standalone]
| From | Martin Gregorie <martin@address-in-sig.invalid> |
|---|---|
| Date | 2011-10-26 00:06 +0000 |
| Message-ID | <j87it8$50h$1@localhost.localdomain> |
| In reply to | #9180 |
On Tue, 25 Oct 2011 15:10:43 -0700, PortisHead wrote: > 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. -- martin@ | Martin Gregorie gregorie. | Essex, UK org |
[toc] | [prev] | [next] | [standalone]
| From | PortisHead <massivetdm850@gmail.com> |
|---|---|
| Date | 2011-10-25 15:21 -0700 |
| Message-ID | <90be2370-ecdc-4da7-839c-26dbeee92e9d@c1g2000vbw.googlegroups.com> |
| In reply to | #9173 |
On Oct 25, 5:47 pm, markspace <-@.> wrote: > On 10/25/2011 5:07 AM, Nigel Wade wrote: > > > On 25/10/11 12:10, PortisHead wrote: 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" ???
[toc] | [prev] | [next] | [standalone]
| From | markspace <-@.> |
|---|---|
| Date | 2011-10-25 15:57 -0700 |
| Message-ID | <j87et9$v0i$1@dont-email.me> |
| In reply to | #9181 |
On 10/25/2011 3:21 PM, PortisHead wrote: > > 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>
[toc] | [prev] | [next] | [standalone]
| From | Lew <lewbloch@gmail.com> |
|---|---|
| Date | 2011-10-25 20:05 -0700 |
| Message-ID | <19801553.19.1319598334254.JavaMail.geo-discussion-forums@prap37> |
| In reply to | #9181 |
PortisHead wrote:
> 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.
--
Lew
[toc] | [prev] | [next] | [standalone]
| From | Nigel Wade <nmw-news@ion.le.ac.uk> |
|---|---|
| Date | 2011-10-26 09:55 +0100 |
| Message-ID | <9gq07mF3mtU1@mid.individual.net> |
| In reply to | #9173 |
On 25/10/11 15:47, markspace wrote: > On 10/25/2011 5:07 AM, Nigel Wade wrote: >> On 25/10/11 12:10, PortisHead wrote: >>> >>> 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. > > > > 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. -- Nigel Wade -- Nigel Wade
[toc] | [prev] | [next] | [standalone]
| From | PortisHead <massivetdm850@gmail.com> |
|---|---|
| Date | 2011-10-26 13:08 -0700 |
| Message-ID | <8dfe3211-bfbf-478e-8b22-cb770d9159b2@f13g2000vbv.googlegroups.com> |
| In reply to | #9198 |
Many thanks to all of you. I really appreciated your help.
[toc] | [prev] | [next] | [standalone]
| From | Roedy Green <see_website@mindprod.com.invalid> |
|---|---|
| Date | 2011-10-27 07:20 -0700 |
| Message-ID | <33qia7p28d6d5330p3rug85o640a95fbv9@4ax.com> |
| In reply to | #9165 |
On Tue, 25 Oct 2011 00:06:16 -0700 (PDT), PortisHead <massivetdm850@gmail.com> wrote, quoted or indirectly quoted someone who said : >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.
[toc] | [prev] | [next] | [standalone]
| From | Lew <lewbloch@gmail.com> |
|---|---|
| Date | 2011-10-27 07:33 -0700 |
| Message-ID | <7180194.1172.1319726029502.JavaMail.geo-discussion-forums@prib32> |
| In reply to | #9165 |
PortisHead wrote:
> 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>
--
Lew
[toc] | [prev] | [next] | [standalone]
| From | PortisHead <massivetdm850@gmail.com> |
|---|---|
| Date | 2011-10-28 12:57 -0700 |
| Message-ID | <089aebad-fdec-491e-afb6-ce32d30b5029@eh5g2000vbb.googlegroups.com> |
| In reply to | #9247 |
On Oct 27, 5:33 pm, Lew <lewbl...@gmail.com> wrote:
> PortisHead wrote:
> > 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.)
From the same directory.That is -----> C:\myclasses\exam\test\dok
>
> > and got the appropriated .class file:
> > Getbcard.class
>
> 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
>
> > 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...>
>
> 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/java...>
> <http://download.oracle.com/javase/7/docs/technotes/tools/windows/java...>
>
> --
> Lew
Thanks again Lew for all the suggestions !!!
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.java.programmer
csiph-web