• 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:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

Classpath: Finding a class without a package.

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello all,

I have the following directory structure:

H:\Users\admin\workspace\P1

now in P1 I have src\pack1\c2.java, where pack1 is a package and c2 is a simple class that extends c1.

I have c1.class in both the P1 directory and in P1\bin\dir1\dir2\c2.class where dir1.dir2 is a package.

When I compile c2 from P1 with the following command I can't find c1.

javac -cp H:\Users\admin\workspace\P1 src\pack1\c2.java

however I it compiles find when I add import dir1.dir2.c1; to c2 and use

javac -cp H:\Users\admin\workspace\P1\bin\ src\pack1\c2.java

So it seems my problem is that when my class isn't in a package the compiler doesn't seem to be able to find it- is this normal behaviour?

Cheers.
 
Ranch Hand
Posts: 300
Eclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I have a blog post about How Classpath works in JAVA , you may find it interesting.
 
Eoin Brosnan
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Javin Paul wrote:
I have a blog post about How Classpath works in JAVA , you may find it interesting.



Javin,
I don't think the answer is contained within your blog but I will have a look at some of the other topics that you covered!

I think the problem is that I am trying to access a class that isn't in a package from a class that IS in a package which doesn't seem to be valid. It really sounds like something that I should of known but that won't be the first time it has happened!
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Eoin Brosnan wrote:I think the problem is that I am trying to access a class that isn't in a package from a class that IS in a package which doesn't seem to be valid. It really sounds like something that I should of known but that won't be the first time it has happened!


Yes, you're right.

If you're in a different package, you always have to use an import statement (or refer to everything as the fully-qualified class name) for any class not in java.lang. However, there is no way to import the default package - once a class is in the default package it can only be used within the default package. One very good reason to always use a specific package.
 
This tiny ad will self destruct in five seconds.
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic