Win a copy of Rust Web Development this week in the Other Languages forum!
  • 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
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Problem while Compiling in Command Prompt(Chapter 10 K&B Book)

 
Ranch Hand
Posts: 125
Scala Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was trying javac and java commands when I stumbled upon this problem:

I wrote these two simple codes and tried to compile and run them through command prompt.
Directory structure is as follows:


Used this to compile PrintStuff from com directory:

which in test made a new folder containing PrintStuff.class


Now I can't compile the SendStuff.java file. I want to use "-d test test/SendStuff.java" from com directory along with -cp which I counldn't figure out how. Please Help.



 
Rancher
Posts: 43027
76
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try something like javac -d test test/*.java.

I find it advantageous to keep source files in directories that reflect their package hierarchy. Once you go beyond a handful of files that is pretty much the only way to keep order anyway.
 
Sidharth Khattri
Ranch Hand
Posts: 125
Scala Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ulf Dittmer wrote:Try something like javac -d test test/*.java.

I find it advantageous to keep source files in directories that reflect their package hierarchy. Once you go beyond a handful of files that is pretty much the only way to keep order anyway.



Me too. All my test codes go in com/source/ and com/classes
Thanks it worked

Two more question.
Can -cp and -d be used on the same command in command prompt? How would I use -cp to solve aforementioned problem?
Something Like:

will this work? I tried searching on google, couldn't find anything.
 
Ulf Dittmer
Rancher
Posts: 43027
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't try searching it, try using it
 
Sidharth Khattri
Ranch Hand
Posts: 125
Scala Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ulf Dittmer wrote:Don't try searching it, try using it



I tired using it, but it didn't work. I'm not sure whether I did something wrong or it is not meant to be used like that, hence I googled it but couldn't find anything.
 
Ulf Dittmer
Rancher
Posts: 43027
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What did you try? TellTheDetails. Saying "it didn't work" when we don't know what, exactly, you tried gives us nothing to work with.
 
Sidharth Khattri
Ranch Hand
Posts: 125
Scala Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sidharth Khattri wrote:

Ulf Dittmer wrote:Try something like javac -d test test/*.java.

I find it advantageous to keep source files in directories that reflect their package hierarchy. Once you go beyond a handful of files that is pretty much the only way to keep order anyway.



Me too. All my test codes go in com/source/ and com/classes
Thanks it worked

Two more question.
Can -cp and -d be used on the same command in command prompt? How would I use -cp to solve aforementioned problem?
Something Like:

will this work? I tried searching on google, couldn't find anything.



^this
To reiterate

Something Like:


Though I couldn't make out what exactly should be the command, can -d and -cp be used together? and how in my aforementioned problem?
 
Sidharth Khattri
Ranch Hand
Posts: 125
Scala Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay, I tried it a lot, can't figure it out, can anyone please help.
Look at this scenario:

Directory Structure:






This will compile fine and make a packA(containing PrintStuff.class) subdirectory inside first:


Now when I try to compile SendStuff.java, obviously it won't work:


so I tried this, which too, won't work:


Can anyone tell me how to compile SendStuff.java file?

 
Ulf Dittmer
Rancher
Posts: 43027
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When I said this:

Ulf Dittmer wrote:I find it advantageous to keep source files in directories that reflect their package hierarchy.


I meant that a source file that is in package XYZ should be in a directory called XYZ. You seem to have opted for a directory organization where not only it is not in XYZ, it is in ABC. That doesn't make things simpler, it complicates things even more.
 
Sidharth Khattri
Ranch Hand
Posts: 125
Scala Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ulf Dittmer wrote:When I said this:

Ulf Dittmer wrote:I find it advantageous to keep source files in directories that reflect their package hierarchy.


I meant that a source file that is in package XYZ should be in a directory called XYZ. You seem to have opted for a directory organization where not only it is not in XYZ, it is in ABC. That doesn't make things simpler, it complicates things even more.



So, isn't there any possible solution if I maintain the directory structure I used?
 
Ulf Dittmer
Rancher
Posts: 43027
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When talking about software, there is almost always a way to do something. But in order to answer that, I'd need you to answer this first: why do you feel the need to do things differently than any other Java developer under the sun? It is such a needless complication that you should have a very good reason for that. And I can't imagine what that might be.
 
Sidharth Khattri
Ranch Hand
Posts: 125
Scala Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ulf Dittmer wrote:When talking about software, there is almost always a way to do something. But in order to answer that, I'd need you to answer this first: why do you feel the need to do things differently than any other Java developer under the sun? It is such a needless complication that you should have a very good reason for that. And I can't imagine what that might be.



I'm just curious because it won't even work when I'll do what you just told in your previous post.

I meant that a source file that is in package XYZ should be in a directory called XYZ.



Like you said, I did this:


PrintStuff.java will compile but SendStuff.java won't.
I used

to compile SendStuff.java which gave same error
 
Ulf Dittmer
Rancher
Posts: 43027
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I'm just curious because it won't even work when I'll do what you just told in your previous post.


Sure it does, but the classpath needs to be correct.

Like you said, I did this:
c:/new/first/packA/PrintStuff.java //source file in the same folder as the package name
c:/new/second/packB/SendStuff.java[/code]


I said to match the directory structure with the package structure. That means files c:/new/packA/PrintStuff.java and c:/new/packB/SendStuff.java

Then SendStuff.java will be compiled without you ever mentioning it in the javac command:

C:/new> javac packB/PrintStuff.java

This only works if the classpath is rooted at the same directory where you're executing this command. You will now get a compiler error for SendStuff.java (which has nothing to do with this issue).
 
Sidharth Khattri
Ranch Hand
Posts: 125
Scala Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ulf Dittmer wrote:

I'm just curious because it won't even work when I'll do what you just told in your previous post.


Sure it does, but the classpath needs to be correct.

Like you said, I did this:
c:/new/first/packA/PrintStuff.java //source file in the same folder as the package name
c:/new/second/packB/SendStuff.java[/code]


I said to match the directory structure with the package structure. That means files c:/new/packA/PrintStuff.java and c:/new/packB/SendStuff.java

Then SendStuff.java will be compiled without you ever mentioning it in the javac command:

C:/new> javac packB/PrintStuff.java

This only works if the classpath is rooted at the same directory where you're executing this command. You will now get a compiler error for SendStuff.java (which has nothing to do with this issue).




Can you tell me the command that you used to compile SendStuff.java as I still couldn't compile it.
I did what you told, directly place packA and packB in "new" folder and then try to compile it.
It's already past 1 day and I still couldn't figure out anything.
So much confusion and problem in this last chapter
 
Ulf Dittmer
Rancher
Posts: 43027
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I used exactly the command I posted. As I said, it results in an unrelated error that you need to fix before compilation will be successful.

Note that saying "I still couldn't compile it." gives us nothing to work with in helping you. Did the computer explode? Where you unable to hit the return key? Did a virus delete all your files? We simply don't know what exactly you did, and what the result was, so we can't speculate what's going on: TellTheDetails.
 
Sidharth Khattri
Ranch Hand
Posts: 125
Scala Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ulf Dittmer wrote:I used exactly the command I posted. As I said, it results in an unrelated error that you need to fix before compilation will be successful.

Note that saying "I still couldn't compile it." gives us nothing to work with in helping you. Did the computer explode? Where you unable to hit the return key? Did a virus delete all your files? We simply don't know what exactly you did, and what the result was, so we can't speculate what's going on: TellTheDetails.



Used this to compile SendStuff.java from c:/new>


My reasoning:
-cp packA: as we need access to PrintStuff.class which is in "c:/new/packA/PrintStuff.class"
packB/SendStuff.java: as compiling from c:/new>

This won't work either:

My reasoning:
hit an trial.
 
Sidharth Khattri
Ranch Hand
Posts: 125
Scala Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay I just got it. I think I need to take a break. I just got so frustrated and overlooked making PrintStuff.java public.
Sorry for bothering you, thanks for your help and sarcastic remarks, not sure you didn't notice it or you were just trolling.

This worked


 
Ulf Dittmer
Rancher
Posts: 43027
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Glad you got it to work. Not sure which of my remarks you thought were sarcastic - I was trying to help you, that is most certainly not trolling.
 
Sidharth Khattri
Ranch Hand
Posts: 125
Scala Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
BTW It's my birthday today :P
so alone
Much Happy So Amazing Wow
 
Ulf Dittmer
Rancher
Posts: 43027
76
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sidharth Khattri wrote:... overlooked making PrintStuff.java public.


Yup, that's what I meant by "You will now get a compiler error for SendStuff.java (which has nothing to do with this issue)." - which I guess should have read "PrintStuff"; that may have been confusing.

And happy birthday!
 
Sidharth Khattri
Ranch Hand
Posts: 125
Scala Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ulf Dittmer wrote:

Sidharth Khattri wrote:... overlooked making PrintStuff.java public.


Yup, that's what I meant by "You will now get a compiler error for SendStuff.java (which has nothing to do with this issue)."

And happy birthday!



Thank you so much
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic