wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Compiling with javac -cp -d, differente packages. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Compiling with javac -cp -d, differente packages." Watch "Compiling with javac -cp -d, differente packages." New topic
Author

Compiling with javac -cp -d, differente packages.

Lorenzo Tagliaferro
Greenhorn

Joined: Jun 15, 2009
Posts: 20

Hello to everyone,

I have a problem to compile two java classes, in two different packages.

This is the source:



...and the other class




When I run the command, I receive an error: cannot find symbol...LoginException

javac -cp classes\org\example\exception -d classes source\org\example\console\*.java



John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
Have you first compiled LoginException.java and have the .class file in the given classpath - classes\org\example\exception
Please move this thread to Beginning Java
John Wintermute
Greenhorn

Joined: Nov 01, 2011
Posts: 14
Hi there

I think your classpath is wrong. Instead of "-cp classes\org\example\exception" try instead "-cp classes". Your LoginException class should have compiled into classes\org\example\exception. It must be in this directory, because its package is org.example.exception - if it's not there, then check how you're compiling the class.

When you're compiling ConsoleExample, the compiler works out the path org\example\exception from the import declaration - you just need to set the classpath to be the folder above 'org', which in your case is 'classes'

cheers, john


ocjp december 2011 -
may peace prevail on earth
Lorenzo Tagliaferro
Greenhorn

Joined: Jun 15, 2009
Posts: 20

Done!!! :-)

Thank you so much John!!! :-))

The problem was the visibility of the LoginException class too!!




Lorenzo Tagliaferro
Greenhorn

Joined: Jun 15, 2009
Posts: 20

I have another issue!

I'm not able to launch the application by the shell!!!

I receive an error to find or load the main class.

This is the command: C:\Console\java -classpath classes ConsoleExample

Someone can help me?! Please!

John Wintermute
Greenhorn

Joined: Nov 01, 2011
Posts: 14
Lorenzo Tagliaferro wrote:I have another issue!

I'm not able to launch the application by the shell!!!

I receive an error to find or load the main class.

This is the command: C:\Console\java -classpath classes ConsoleExample

Someone can help me?! Please!



OK, you've given the JVM the correct classpath, now you have to give it the fully-qualified name of the class you're trying to run The above command line would only work if ConsoleExample.class file was in the 'classes' folder, and was not part of any package. As an experiment, copy ConsoleExample.class into 'classes', run the same command line and you'll get a different error (something like 'wrong name').


(If my clue above wasn't enough, try "C:\Console\java -classpath classes org.example.console.ConsoleExample )
Lorenzo Tagliaferro
Greenhorn

Joined: Jun 15, 2009
Posts: 20

Excellent explanation!! :-)

But, if i try to launch the application i receive the "infamous" error: (wrong name: org/example/console/ConsoleExample)

I've tried to digit this command: java -cp classes\org\example\console\;classes\org\example\exception ConsoleExample

Thank you so much!
John Wintermute
Greenhorn

Joined: Nov 01, 2011
Posts: 14
Lorenzo Tagliaferro wrote:Excellent explanation!! :-)

But, if i try to launch the application i receive the "infamous" error: (wrong name: org/example/console/ConsoleExample)

I've tried to digit this command: java -cp classes\org\example\console\;classes\org\example\exception ConsoleExample

Thank you so much!


Hi there

You need to tell 'java' the fully-qualified name of the class you're trying to run, which in this case is org.example.console.ConsoleExample. The JVM knows that this class must be in a file called ConsoleExample.class, and it must be in a folder structure org\example\console because of the package name. The -cp option tells it where to find the 'org' folder

So your full command should be: java -classpath classes org.example.console.ConsoleExample

Cheers, John
Lorenzo Tagliaferro
Greenhorn

Joined: Jun 15, 2009
Posts: 20

Now it works!!! :-))

Thank you so much John!!!

I hope to repay you soon!!!
John Wintermute
Greenhorn

Joined: Nov 01, 2011
Posts: 14
Lorenzo Tagliaferro wrote:Now it works!!! :-))

Thank you so much John!!!

I hope to repay you soon!!!


It's my pleasure I spent quite a while experimenting with this topic, moving files around, creating JAR files and generally trying lots of variations. It certainly helped in the exam.
Lorenzo Tagliaferro
Greenhorn

Joined: Jun 15, 2009
Posts: 20

I agree with you!

I wrote a lot of code, by the IDE. And now i realize, that i don't remember anything!!!

Bad choice!!! Really bad choice...

It's hard to write code by Notepad++ or Gedit, but this is the best way to understand how Java works!!


Thank you again for your support!!

Maybe you'll read me again in some topics!! ;-) Desperate HouseCoder!!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Compiling with javac -cp -d, differente packages.