Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

getInputStream - relative path

 
Dilip kumar
Ranch Hand
Posts: 360
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello All,

I have a jar file (GLUE-STD.jar) which has a class JARLoader in package electric.xml.io.mapping

The JARLoader class has following code

inputstream = ClassLoaders.getInputStream("file:/".concat(String.valueOf(String.valueOf(s))));

Basically above code reads mappings.jar which is currently located in c:\

I need to modify the above code in such a way that the JARLoader class should look for mappings.jar in the same folder that has GLUE-STD.jar using relative path not physical path so that app finds the jar file while running on Windows as well as Linux environment.

Really appreciate any suggestions.

Thanks
 
Amit M Tank
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
put your folder in the classpath. Use the following code.



Hope this helps.
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Amit:


(The getClass() method is not static so the above code will not compile. I think this was a typo but just wanted to point out.)

There is a subtle difference between using getResourceAsStream() in java.lang.Class and the one in java.lang.ClassLoader.
The former method expects the requested resource to be present in the classpath in the directory structure resembling the package name of the class. So, if your class name is foo.bar.MyClass then the requested resource must be present under foo/bar/ under any of the directories in the classpath.
The method in the ClassLoader expects the resource to be present directly in the classpath.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What are you going to do with the InputStream and why? I have the vague feeling that there might be a better way to solve the problem you are trying to solve...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic