• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Java file directory structure

 
Ranch Hand
Posts: 235
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've decided to lose my IDE and start coding with a text editor. Apparently understanding java and javac are on the certification exam, and I need to learn it anyway. (I only used the IDE for the syntax highlighting and having the error messages in the debug window). Java and javac run fine. Just a simple Hello World app.



Program compiles, with no errors, and runs just like one would expect. What confuses me, though, is that the app runs fine, as long as I run it from the class directory. If I attempt to run it from any other directory it returns the Error: Could not find or load main class <pathname>.

CLASSPATH = ;.
Windows 8.1
java -version: 1.8.0-ea

Anyway, I'm lost and confused. Couldn't tell you how many sites I looked through. Seems most just assume one is using the IDE, and those that actually give a directory structure only show a partial as it relates to packages.
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The root cause is CLASSPATH
Classpath is the path (or a bunch of paths) given to the JVM as probable locations where the required classes might be found. In addition to these, the JVM will also look in the current directory for the required classes (which explains why it runs from the current directory)
For more information, you can refer to https://coderanch.com/how-to/java/HowToSetTheClasspath

Robert D. Smith wrote:(I only used the IDE for the syntax highlighting and having the error messages in the debug window)


I would say, getting rid of the IDE at a beginners stage is a very smart thing to do. In case you prefer highlighting (I agree, can be very useful) you might want to consider some advanced editors like JEdit which can do the highlighting/indentation for you, but are not full blown IDEs (in the sense that they do things under the hood, things, which as a developer, you need to know)
 
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Maneesh Godbole wrote: . . . In addition to these, the JVM will also look in the current directory for the required classes . . .

I thought it only looks in the current directory by default if no CLASSPATH has been set elsewhere. The CLASSPATH ;. obviously interferes with its finding the necessary files.
 
Robert D. Smith
Ranch Hand
Posts: 235
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the replies. The sad thing is I saw a handful of videos on setting up the paths on youtube and all of them set theirs up that way. And thanks for the link; I don't know how I missed that because the FAQs were the first place I turned.
 
Bartender
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To keep things simple, you shouldn't need to set classpath when compiling or running your programs. Unless you use external libraries.

One thing to remember is wherever you execute your command is where the compiler/jvm will look. So the actual java or class files is "relative" to your current directory.
reply
    Bookmark Topic Watch Topic
  • New Topic