• 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
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

Running class in package is failing if directory hierarchy does not match package declaration.

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
Lets say we are in directory ~/home/playground
We create class Goose in the folder above like the following:



I compile the class like the following:
javac Goose.java

the Animal.class is generated (always in the same directory)

But running it as below is failing with error: Error: Could not find or load main example.Goose


Also running the command below is failing with the same error:


Any idea why ?

PS: I intentionally didn't want to create a folder my-package and put my class in it. I just want to figure outwhy compiling is successful. But Running the code is failing

Thanks in advance.

 
Marshal
Posts: 74341
334
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

Reb orn wrote:. . . I intentionally didn't want to create a folder my-package . . .

But you must have the XXX.class file in a folder corresponding to the package name. Otherwise it won't run.
 
Saloon Keeper
Posts: 1603
51
Eclipse IDE Postgres Database C++ Java
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your .java source files can be wherever, they do not need to follow the package hierarchy, you can have every one of them sitting in one directory if you so choose, as long as you compile with javac -d <root of directory structure for class file output>  If you do not, the .class file for each .java file is placed in the exact same directory as the .java source it came from.

You have no such liberty with .class file location, whether you are putting them into a jar or accessing them from a directory structure, the .class file needs to be found where its corresponding package says it should be.  This is how Java finds things at runtime, so doing anything else is hiding your .class file from java!  There are two basic ways to do this, have your .java source files sitting in the appropriate directory structure (which is not otherwise required) or using -d as above to have the java compiler place them where they need to be in terms of the package structure under the target directory you provide.

 
T Allay
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Campbell Ritchie and Jesse Silverman. This makes sense.
Any resources that mention .class files should follow the folders structure?
 
Campbell Ritchie
Marshal
Posts: 74341
334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

T Allay wrote:Thanks . . .

That's a pleasure

Any resources that mention .class files should follow the folders structure?

Yes. If you use the unnamed package, there is no specific name for the folder the XXX.class files are in, and you usually use the current directory.
 
Jesse Silverman
Saloon Keeper
Posts: 1603
51
Eclipse IDE Postgres Database C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi:

Campbell, I think our poster may have meant something like this:
https://docs.oracle.com/javase/tutorial/java/package/managingfiles.html

That is, "are there any free canonical learning sources that explain these things rather than presuming you already know it?"

In other recent replies, I have mentioned that use of the unnamed package for anything but one-off throwaways or absolute toy examples is to be discouraged, which I know is agreed upon here.
 
Campbell Ritchie
Marshal
Posts: 74341
334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jesse Silverman wrote:. . . I think our poster may have meant something like this . . .

Yes, I think you are right.
 
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