-
1. Re: Class path not loading from web-inf/classes
sfcoy Jul 5, 2012 6:37 AM (in response to martsecurit)Martin Thorpe wrote:
...
getClass().getClassLoader().getResource("/myfile.ext");
...
This returns a URL. What are you doing with this URL?
-
2. Re: Class path not loading from web-inf/classes
martsecurit Jul 5, 2012 9:30 AM (in response to sfcoy)Hello thanks for your response.
Yes it does but the URL is incorrect as it is pointing to bin/content which does not exist.
From this URL I am reading the path to create a file
new File(myURL.getPath())
Works like a charm in WebSphere 6.1, 7 and 8.
cheers
-
3. Re: Class path not loading from web-inf/classes
sfcoy Jul 5, 2012 11:27 AM (in response to martsecurit)I know this works in other servers (I use them too), but it's not required to. An application server can feasibly deploy your archives without unpacking them at all. Where will your java.io.File be then?
You should be using something like java.lang.ClassLoader#getResourceAsStream(java.lang.String) instead.
This is completely portable and will work everywhere.
-
4. Re: Class path not loading from web-inf/classes
martsecurit Jul 5, 2012 11:49 AM (in response to sfcoy)Thanks for your response.
Where are the compiled java classes (servlets, filters etc) for the WAR being loaded from then? It is web-inf/classes surely? Will JBoss only load .class files from that directory?
If that is the case what is the standard approach, when working with JBoss, regarding locations to store and load .properties files and the such from the class path?
I mean local to a WAR application not global to the server also.
Thanks for reading.
-
5. Re: Class path not loading from web-inf/classes
martsecurit Jul 6, 2012 2:56 AM (in response to sfcoy)Hello Stephen, all.
Ok referring to some docs it states that I should place my files in the web-inf/classes.
see here:
https://docs.jboss.org/author/display/AS7/How+do+I+migrate+my+application+from+AS5+or+AS6+to+AS7
In previous versions of AS, the JBOSS_HOME/server/<servername>/conf/ was available in the classpath. Hence the properties files in that location were available in the classpath of the application.
In AS7, to get those properties available in the classpath, package them within your application. For example, if you are deploying a .war then package those properties in WAR WEB-INF/classes/ folder. If you want those properties accessible to all components in a .ear, then package them at the root of some .jar and place that jar in EAR lib/ folder.
So according to the docs these should be available on the class path. So why is it referring to them found in bin/content? Still bemused.
regareds
-
6. Re: Class path not loading from web-inf/classes
martsecurit Jul 6, 2012 4:53 AM (in response to sfcoy)Hello
getResourceAsStream works. But I don't like having to change my code just for a specific container but that is life I guess.
getClass().getClassLoader().getResourceAsStream("/myFile")
Thanks for your help Stephen
-
7. Re: Class path not loading from web-inf/classes
sfcoy Jul 6, 2012 5:31 AM (in response to martsecurit)Hey Martin
Martin Thorpe wrote:
Hello Stephen, all.
...
So according to the docs these should be available on the class path. So why is it referring to them found in bin/content? Still bemused.
regareds
You keep trying to think of these things as individual files in your file system. They are not. If you look at the URL returned by your call to:
getClass().getClassLoader().getResource("/myfile.ext");
you will see that it is not a file://path/to/my/resource.
If you're looking to migrate property files from JBoss AS 5 conf directory, have a look at How to put an external file in the classpath. This still requires the use of getResourceAsStream.
-
8. Re: Class path not loading from web-inf/classes
sfcoy Jul 6, 2012 5:33 AM (in response to martsecurit)Martin Thorpe wrote:
... But I don't like having to change my code just for a specific container but that is life I guess.
...
The fact that your existing code worked before is just a happy accident. It's not portable in any way.
-
9. Re: Class path not loading from web-inf/classes
martsecurit Jul 6, 2012 5:44 AM (in response to sfcoy)Stephen Coy wrote:
Martin Thorpe wrote:
... But I don't like having to change my code just for a specific container but that is life I guess.
...
The fact that your existing code worked before is just a happy accident. It's not portable in any way.
Happy accident that it works only between 3 different application servers and 2 releases of Java ;-)
-
10. Re: Class path not loading from web-inf/classes
astempfel Jul 10, 2013 3:59 AM (in response to martsecurit)hello!
what was your solution to load the files/directories correctly? i have the same problem..
regards
angela
-
11. Re: Class path not loading from web-inf/classes
martsecurit Jul 10, 2013 4:04 AM (in response to astempfel)Hello Angela it is the one that I marked as the correct answer in the post.
-
12. Re: Class path not loading from web-inf/classes
astempfel Jul 10, 2013 4:13 AM (in response to martsecurit)thanks for the quick answer! i tried that as well, it returns a org.jboss.vfs.VirtualJarInputStream. How do you access the files/directory then? because reading it with a BufferedReader doesn't work for me
BufferedReader rdr = new BufferedReader(new InputStreamReader(resourceAsStream));
String line;
while ((line = rdr.readLine()) != null) {
System.out.println("file: " + line);
}
rdr.close();
regards
Angela
-
13. Re: Class path not loading from web-inf/classes
martsecurit Jul 10, 2013 4:18 AM (in response to astempfel)What does not work? do you get an exception?
If I go here I get a load of examples.
-
14. Re: Class path not loading from web-inf/classes
astempfel Jul 10, 2013 4:24 AM (in response to martsecurit)no exception, but the readline method returns null. so it seems that the directory can't be read...