GetResourceAsStream not working on Websphere server - java

Application Server : IBM Websphere
Java 6
Issue :
Below is the folder structure of web application :
WEB-INF/libs/props/
The issue is below code is not working :
this.getClass().getResourceAsStream('/props'/+fileName) where fileName is a valid file name inside props folder of WEB-INF/lib/pros.
The class which is calling above method is inside a JAR and included in lib
The above call returns NULL.
The same set up works on tomcat with web application deployed in expanded mode integrated inside Inteliij IDE. On environment where this issue is coming , the application is deployed as ear
Please help as I not able to get any clue on this

The jar files under WEB-INF/lib are in the classpath. Not WEB-INF/lib itself.
The file should be in one of those jars, or under WEB-INF/classes (which is in the classpath)

Related

Can not find WAR file path after deployment using tomcat 8 manager

I have been testing my tomcat 8 setup on debian 9. Everything seems fine until I try to deploy a helloServlet WAR file through tomcat 8 manager. I created the very simple example program following the instruction here.
I use tomcat manager to upload the HellowServlet.war from my working directory. Then I can access it though http://localhost:8080/HelloServlet/sayhello
The program works just fine. However, no matter how I search (/etc/tomecat8 /usr/share/tomcat*), I just can not find the war file or any components inside of it. I really want to know the exact location of my application file. Anyone may tell me where the application files are stored by the tomcat 8 mananger?
More update:
I just tried to upload the same war file again, and I got following message from tomcat
FAIL - War file "HelloServlet.war" already exists on server
That means the war file is sitting somewhere on the server, but the path is unknown to me.
Found it.... it's inside /var/lib/tomcat8/webapps
However, I can not find any configuration file in tomcat 8 describing that it uses /var/lib/tomcat8 as one of its working directory.

Loading file from application .ear deployed on weblogic

I'm trying to write code that will load *.html file from application .ear and display it. The structure of my application is:
application
-src
-main
-java
-my.package.few.folders
-Class.java
-webapp
-my.html
I'm new in Java and weblogic so I'm not able to figure it out by myself and all threads I found in internet are mostly the same. File should be accessible using:
Class.class.getClassLoader().getResource("main/webapp/my.html");
The problem is that my application is deployed on weblogic and above code gives me path to a weblogic domain (not my application ear where are *.html files). So using this code I'm always getting null pointer exception because .ear is hidden inside few folders in weblogic domain. I want to avoid giving specified path to .ear in weblogic, because it need to work on different servers and with only managed server name changed it will crash. How can I get straight into my .ear, not through weblogic domain?

Is there any way to configure tomcat servlet working directory?

When I'm running my web-application in tomcat and of course it's classes having tomcat working directory, because the JWM was started there. But is there any way to configure tomcat, as all classes of each deployed application, will have the application directory as their working directory?
You don't have to configure Tomcat working directory. You can get your application root directory from your servlet by calling :
String root = getServletContext().getRealPath("/");
Note that this folder is accessible from client browser.
If you want a working directory that is inaccessible from client, create a folder inside WEB-INF
String privateRoot = getServletContext().getRealPath("/WEB-INF");
You can change Tomcat's startup scripts. The tomcat process inherits whatever the current directory was for the startup script. To make tomcat start in a different working directory, you'll have to figure out what is launching tomcat, and change that process to change to the desired directory before it runs startup.sh.
You can look here on how to setup the working directory.

java.lang.NoClassDefFoundError: WEB-INF/classes/

I am getting an error when deploying an EAR file which contains a web service and a web application. I get a NoClassDefFoundError when I deploy the application because the server is looking in the wrong folder for the class file.
The server (WebLogic) thinks the class file should be in WEB-INF/classes, but it is in <project-root>/classes.
How do I change where WebLogic should look for class files? I already tried the classpath, doesn't work.
That is happening because this is the structure of the web application according to the specification. Check this out: https://docs.oracle.com/javaee/6/tutorial/doc/bnadx.html

Difference between deploying WAR and Build folder

Question 1: May I ask what is the difference between deploying a java webapp with it's WAR file vs just copy/pasting the build folder into tomcat webapp folder?
Question 2: Somehow I am told to deploy my project just by renaming my /build/web folder to /build/, then copy and paste this folder into tomcat/webapp folder. Tomcat did serve the web app and I could access it via url. But the problem is that I suspect my System variables were not set. I start up a servlet and put this code in this init(ServletConfig config) method:
System.setProperty("LogPath","D:/Test/logs");
And doing this in my log4j.properties
log4j.appender.file.FILE=${LogPath}/wrapper.log
wrapper.log is not found in the designated directory but a stdout.log is found in tomcat/logs folder.
I am sure the init() method was fired because I have a quartz scheduler there. I am suspecting that my System.setProperty was not set. Any hint?
Update: With all the same source code, I have no problem if I am deploying with a WAR file. The ${LogPath} in log4j.properties work as expected.
Let me answer you the first question.
WAR file is a zip archive with different name. When you deploy this file to the Tomcat server, it unpacks this file to its folder as you would do it by copy-paste. If you are just developing your own project in your own environment and you don't want to distribute it, you don't need to create a war file. But if you want to distribute this project, I recommend you to create a war file. One file is easier to be sent.
Read more on Wikipedia http://en.wikipedia.org/wiki/WAR_%28file_format%29

Categories