• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

package test

 
Ranch Hand
Posts: 234
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI guys, I' ve picked up the book for the OCA exam, and I was just reading about packages. I tried the package access code sample, but, strangely, it compiles OK but it won't run. when I try I get:
Error:COuld not find or load main clas package.ClassB
So I have a temp folder C:\temp and C:\temp\packagea containing ClassA.java


and C:\temp\packageb containing ClassB.java


Any idea?
thanks
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The java command expects a fully-qualified class name and not a filename. (Unlike the compiler, javac, which expects a filename of a source file).

To run ClassB, use the command: java packageb.ClassB
(with a dot, not a slash).

There are a few things to remember with regard to packages:

1. The directory structure has to match the packages of your source files. It looks like you have that correct.

2. When you run a program with packages, then make sure you have the base directory of the package hierarchy in the classpath. If you don't specify the classpath explicitly (through the -cp or -classpath option or the CLASSPATH environment variable), then Java will take the current directory as the classpath, so then you should run your program from the base directory of the package hierarchy. In your example, you should be in the directory C:\temp on the command line.

So, this should work:

C:\temp> javac packagea\ClassA.java
C:\temp> javac packageb\ClassB.java

C:\temp> java packageb.ClassB
 
Jason Attin
Ranch Hand
Posts: 234
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks. Apologies, just realized that in fact I was using the right command but didn't work.
Anyway, I was compiling both the classes in both the packages at the same time, but now instead I tried to compile them separately:



But when i compile ClassB I get this:
 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That means that the current directory is not in the classpath. Did you set the CLASSPATH environment variable to anything? It's best to leave that variable unset.

If, for whatever reason, you do want to set it, then make sure that you add the current directory to it, which is indicated by a "." (point): CLASSPATH = C:\Blah\Whatever;.

Or, if you don't want to change CLASSPATH, then use the -cp or -classpath option when compiling and running your program:

C:\temp> javac -cp . packageA\ClassA.java
C:\temp> javac -cp . packageB\ClassB.java

C:\temp> java -cp . packageB.classB
 
Ranch Hand
Posts: 91
3
Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey, I have the same book.  I followed along and it worked fine.

Can you do a dir /s and copy/paste it here?

Before:


After:
 
Jason Attin
Ranch Hand
Posts: 234
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah wait, the CLASSPATH variable, yes you're right sorry. Usually I include the path to the Java installation, and I forgot to do that here. Currently it is set to C:\Program Files (x86)\IBM\RationalSDLC\ClearQuest\cqjni.jar only, so I will add the path to java as I usually do. Let me try that first, failing that I will use .
 
Jason Attin
Ranch Hand
Posts: 234
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, so changing the path variable doesn't work (although I changed to point to the directory where java is installed). So I tried this approach instead

C:\temp> javac -cp . packageA\ClassA.java
C:\temp> javac -cp . packageB\ClassB.java

C:\temp> java -cp . packageB.classB


which worked. Now, the problem is that this is different from what the OCA book says, so what do I do? which one should I follow?
thanks
 
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why have you set the CLASSPATH environment variable?

There's almost never a good reason to do this in any environment, especially not a dev one.

You also never need to add the Java installation directory in a classpath either.
 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Note that the variables PATH and CLASSPATH are two totally different things.

The PATH is used by Windows to find executable programs to run. You should add the bin directory of the JDK to the PATH so that you can use the javac and java commands from the command prompt.

The CLASSPATH is used by Java to find *.class files. It's best to leave this not set at all, as already explained.
 
Julian West
Ranch Hand
Posts: 91
3
Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Perhaps your CLASSPATH is set to something that doesn't include the current directory (.).

To check, at DOS prompt:


To add current directory to current CLASSPATH:


To clear it:


cf. https://docs.oracle.com/javase/tutorial/essential/environment/paths.html

DOS command reference: http://ss64.com/nt/set.html
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic