File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Compiling with javac -cp -d, differente packages.

 
Lorenzo Tagliaferro
Greenhorn
Posts: 20
Eclipse IDE Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 1776
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Lorenzo Tagliaferro
Greenhorn
Posts: 20
Eclipse IDE Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Done!!! :-)

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

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




 
Lorenzo Tagliaferro
Greenhorn
Posts: 20
Eclipse IDE Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 20
Eclipse IDE Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 20
Eclipse IDE Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now it works!!! :-))

Thank you so much John!!!

I hope to repay you soon!!!
 
John Wintermute
Greenhorn
Posts: 14
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 20
Eclipse IDE Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic