• Post Reply Bookmark Topic Watch Topic
  • New Topic

Finding source files in other directory for javac with -cp option  RSS feed

 
Ranch Hand
Posts: 42
Eclipse IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm having another issue. I have 2 java source files(see below). They are place in the same directory. How do I compile them using classpath?

I have already tried 1st attempt : javac -cp com.companyname.interview.DuplicateReplace.java DuplicateReplaceTest.java [did not work!]
2nd attempt: javac -cp DuplicateReplace.java DuplicateReplaceTest.java [again, did not work!]







Sorry if I'm being stupid here as usual. Just let me know what am I doing wrong.


 
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Either compile from the root of the file system you are using, or navigate to the directory those source files are in. DuplicateReplace appears to be a dependency for DuplicateReplaceTest, so you should compile DuplicateReplace first. From the root write
javac com.company.interview.DuplicateReplace.java
and inside the directory write
javac DuplicateReplace.java
Tell us what happens like that.

If those files are in the same tree you probably don't need the -cp flag. Those classpaths don't look right, anyway.
 
Arnob Dey
Ranch Hand
Posts: 42
Eclipse IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It seems I had a complete misconception of how packages and corresponding import statements works. While I'm still working on it. Please help me understand the following:

See my directory structure below:

|--Code
.........|--Java
..................|--SourceFiles
..................|--ClassFiles

[Sorry, the previous program got its DNA altered and moved. But I found another program and modified it based on my needs.]

StringReversal.java@SourceFiles



StringReversalTest.java@Java [Had to be moved from SourceFiles.]



Now comes compiling:

Current Location@Code> javac -d Java Java/SourceFiles/StringReversal.java [success!]

Current Location@Code>javac -d Java Java/StringReversalTest.java [Failed: could not find package.] (why?)

Current Location@Java> javac StringReversalTest.java [success!] (then why the previous attempt failed?)


Thanks in advance...:-)

 
Campbell Ritchie
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are you using the -d option? That has nothing to do with classpaths, but instructs the compiler to put the .class files in (new) directories. Writing Java means you start looking for the source file (XXX.java) in the Java folder. Do you actually have a Java folder? Is that where the source files are?
 
Arnob Dey
Ranch Hand
Posts: 42
Eclipse IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes there is a "Java" folder under "Code" (See the directory structure in my previous post). And I used -d(for destination) to place the class files in specified folders. I understand that it has nothing to do with classpath.

Under the "Java" folder we have the "SourceFiles"(this is where the .java files are usually kept) and "ClassFiles"(this is where the .class files are usually kept).

Current Location@Code implies I'm attempting to compile it while being at "Code" folder.

StringReversal.java@SourceFiles implies StringReversal.java is located in folder "SourceFiles".



Note: Maybe I should have named the folders following the convention "src" and "classes" as what you might expect but I don't want to change it now cause as such might make it even more confusing.
 
Sheriff
Posts: 11498
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Given the package declaration you made, the file paths should be these:

/Code/Java/com/companyname/interview/DuplicateReplace.java
/Code/Java/com/companyname/interviewtest/DuplicationReplaceTest.java

You would run your javac and java commands from the Java directory
 
Arnob Dey
Ranch Hand
Posts: 42
Eclipse IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Junilu Lacar: Please go through the message thread...:-)
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!