• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

compiling package  RSS feed

 
Ranch Hand
Posts: 94
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi javaranch.

I'm trying to compile with javac two classes from two different packages, but I'm not able to:
Following the filepath:
C:\sources\packageOne\Parent.java
C:\sources\packageTwo\Child.java

Parent is:


Child is:


First I try to compile the Parent:
C:\sources>javac packageOne\Parent.java
and it succeded

Then I try to compile the Child:
C:\sources>javac packageTwo\Child.java
and outputs the following errors:

packageTwo\Child.java:2: error: package packageOne does not exist
import packageOne.*;
^
packageTwo\Child.java:3: error: cannot find symbol
public class Child extends Parent{
                           ^
  symbol: class Parent
2 errors

What's the mistake?

 
Sheriff
Posts: 12343
201
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try running javac with the -classpath option. https://docs.oracle.com/javase/8/docs/technotes/tools/windows/classpath.html
 
Daniele Barell
Ranch Hand
Posts: 94
1
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It works!

C:\sources>javac -cp . packageTwo\Child.java

Thanks a lot Junilu!
 
Marshal
Posts: 60136
188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are several ways to compile classes inside packages. This is how I would suggest you do it, working from the parent directory for your packages:-
  • 1: Create folders matching the package names: mkdir packageOne
  • 2: Edit the classes: myEditor packageOne/MyClass.java
  • 3: Save the .java file
  • 4: Compile with javac packageOne/MyClass.java You now have the .java file and the .class file in the same directory, and the CLASSPATH will include your packages by default.
  • As I said, there are other ways to do it.
     
    Daniele Barell
    Ranch Hand
    Posts: 94
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Campbell Ritchie wrote:There are several ways to compile classes inside packages. This is how I would suggest you do it, working from the parent directory for your packages:-

  • 1: Create folders matching the package names: mkdir packageOne
  • 2: Edit the classes: myEditor packageOne/MyClass.java
  • 3: Save the .java file
  • 4: Compile with javac packageOne/MyClass.java You now have the .java file and the .class file in the same directory, and the CLASSPATH will include your packages by default.
  • As I said, there are other ways to do it.



    Hi Campbell,
    Thanks for the patient answer!

    I tried your method but something went wrong:
    First I compile Parent (same code posted before):
    C:\OCA\sources>javac packageOne/Parent.java
    And it succeded.

    But when I try to compile Child...


    C:\OCA\sources>javac packageTwo/Child.java

    It fails:
    packageTwo\Child.java:2: error: package packageOne does not exist
    import packageOne.Parent;
                    
     
    Campbell Ritchie
    Marshal
    Posts: 60136
    188
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Did you create a directory called packageTwo? Did you move the .java file into it?
     
    Daniele Barell
    Ranch Hand
    Posts: 94
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Yes, I did.
     
    Campbell Ritchie
    Marshal
    Posts: 60136
    188
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    In which case, can you see the two directories from the command line when you use the dir command? It is dir on Windows® and ls on *nix, but you appear to be on Windows®.
     
    Daniele Barell
    Ranch Hand
    Posts: 94
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    It's Windows.
    And, yes, directory are visible:
    C:\OCA\sources>dir
    Il volume nell'unità C non ha etichetta.
    Numero di serie del volume: 4CB4-9433

    Directory di C:\OCA\sources

    09/10/2017  12:47    <DIR>          .
    09/10/2017  12:47    <DIR>          ..
    09/10/2017  12:51    <DIR>          packageOne
    09/10/2017  12:53    <DIR>          packageTwo
                   0 File              0 byte
                   4 Directory  243.107.758.080 byte disponibili

     
    Master Rancher
    Posts: 3630
    40
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    This looks like you have a CLASSPATH environment variable defined.
    That would explain why it can't find the other package, even though you are calling javac inside the correct directory.
     
    Campbell Ritchie
    Marshal
    Posts: 60136
    188
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Good point: try the -cp . option and see whather that improves things.

    It usually does more harm than good to set a system CLASSPATH.
     
    Junilu Lacar
    Sheriff
    Posts: 12343
    201
    Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    OP was already successful with the -cp . option early on. I think OP was just trying to see if Campbell's suggestion would work for her, too.
     
    Daniele Barell
    Ranch Hand
    Posts: 94
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Dave Tolls wrote:This looks like you have a CLASSPATH environment variable defined.
    That would explain why it can't find the other package, even though you are calling javac inside the correct directory.



    Hi Dave.
    Yes I had CLASSPATH environment variable set elsewhere.

    Campbell Ritchie wrote:Good point: try the -cp . option and see whather that improves things.

    It usually does more harm than good to set a system CLASSPATH.



    So it would be better to take out...ok.

    Junilu Lacar wrote:OP was already successful with the -cp . option early on. I think OP was just trying to see if Campbell's suggestion would work for her, too.


    Exactly, I wanted to follow Campbell's suggestions but I couldn't. Now I also understand why.

    Thanks again to everyone!
     
    Campbell Ritchie
    Marshal
    Posts: 60136
    188
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Junilu Lacar wrote:OP was already successful with the -cp . option early on. . . .

    Sorry, I missed that.
     
    Campbell Ritchie
    Marshal
    Posts: 60136
    188
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Yes, delete the system CLASSPATH; if you ever need to set a CLASSPATH it will be different for each application, so use the -cp option.
     
    Daniele Barell
    Ranch Hand
    Posts: 94
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Campbell Ritchie wrote:Yes, delete the system CLASSPATH; if you ever need to set a CLASSPATH it will be different for each application, so use the -cp option.


    I will!
     
    Daniele Barell
    Ranch Hand
    Posts: 94
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Daniele Barell wrote:

    Campbell Ritchie wrote:Yes, delete the system CLASSPATH; if you ever need to set a CLASSPATH it will be different for each application, so use the -cp option.


    I will!


    One last note.
    I deleted the System CLASSPATH and now it works without -cp .


    C:\OCA\sources>javac packageOne/Parent.java
    C:\OCA\sources>javac packageTwo/Child.java

    No error.

    Then I completed Child extending Parent and giving the main pass:

    recompiled.

    To run it I used java command in this way:
    C:\OCA\sources>java packageTwo.Child
    That results:
    My name is: Daniele Barell


     
    Campbell Ritchie
    Marshal
    Posts: 60136
    188
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Success
    If you delete the system CLASSPATH, the java and javac tools will default to using . as their classpath.
     
    Campbell Ritchie
    Marshal
    Posts: 60136
    188
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thank you for the pie
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!