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

CLASSPATH Tutorial

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

After having pretty successfully used what I consider to be some fairly advanced java concepts in my current project, I was distraught to find, after having created a few self-executable JAR's, that my education contained an obvious hole. Allow me to explain...

Some of the objects I use read static text files, which inform the creation of other objects. The problem is that after I JAR my files (which I do using Eclipse, so the process is little more than a click or two), the text files are no longer read correctly. Having researched the issue a bit I have determined that this is probably due to the fact that they cannot be found, due to their CLASSPATH being unknown or incorrect. However, I do not understand what the nature of CLASSPATH is to an extent that I can troubleshoot the issue further.

Additionally, my project originally used RMI as part of the architecture. However I ended up dropping RMI in favor of simple socket input and output streams, at least partly due to more CLASSPATH troubles, this time in respect to stub/skeleton setup.

Any tutorial recommendations on filling in this unfortunate knowledge gap?

Oh, and I fear also that my reliance on Eclipse belies an additional misunderstanding of JAR mechanics...
 
Marshal
Posts: 79699
381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Find the Java Tutorials(I haven't got a link at present, sorry) and look for the section called "deployment;" it tells you how to add files to a .jar. So you might be abl to include them in the .jar and not need to set a CLASSPATH. It also tells you how you can specify a CLASSPATH inside the .jar.
 
Campbell Ritchie
Marshal
Posts: 79699
381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
By the way, this is probably too complicated a subject for beginners. Moving
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Alex,

Well, first things first. Do you read your text files with FileReader or FileInputStream? If so, then if the files are in a JAR they're not going to be found, period. No CLASSPATH setting is going to help here, as those classes read files on disk, period, not entries in JAR files.

The alternative is to use the Class.getResource() / ClassLoader.getResource() / getResourceAsStream() family of methods. This is what you want to research. Basically, you put your text files in the same diretory hierarchy as your classes, and these methods will find them whether that hierarchy is on disk or in a JAR file.
 
Alex Birmingham
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks guys, I feel like that gives me a better understanding of where I need to focus my research (I do indeed use a FileReader.)

Apologies for the obtuse question which belied that I didn't really know what to ask.
 
An elephant? An actual elephant. Into the apartment. How is the floor still here. Hold this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic