• Post Reply Bookmark Topic Watch Topic
  • New Topic

CLASSPATH Tutorial  RSS feed

 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • 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: 56608
172
  • Mark post as helpful
  • send pies
  • 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: 56608
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By the way, this is probably too complicated a subject for beginners. Moving
 
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!