0

I am facing a problem by deploying a War file on a Tomcat Server. My Tomcat Server configuration as follows:

tomcat config

My steps to deploy:

  1. I created a WAR file in Eclipse Kepler (Windows 7 64 bit, no Maven used).
  2. Uploaded it with a Tomcat Web Application Manager.
  3. Clicked deploy.

Tomcat unpacks WAR file in tomcat/webapps and throws me NullPointerException. It is missing some files that should be imported to project to work it right.

When I was working in Eclipse, as a source for these files I gave a workspace path, given in the Server settings and adding the wtpwebapps folder, which was offered by Eclipse by default. So my complete path looks like this:

C:\Users\myUserName\kepler\workspace.metadata.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\myProjectName\WEB-INF\classes\project\fdsProject\FDS.prj

Now, when I deploy the project, it locates in a webapps folder, not in wtpwebapps and if I try to start in a browser, it shows me NullPointerException. But as soon as I copy the wtpwebapps folder from my workspace to the tomcat installation folder, near webapps, my project is starting successfully.

It means that I cannot start my project on another server, just deploying a standalone WAR file. I must add manually the wtpwebapps with my project in into the tomcat server installation folder. How can I optimize it and start project just with a WAR file deployed? Thank you!

EDIT

My project structure is looking like this: project structure

  • thanks for your comment, but I posted a screenshot of my server configuration, no code presented in this issue – Ildar Allayarov Apr 07 '17 at 13:41
  • What's the NPE stack trace? How are you referring to those files? – nitind Apr 07 '17 at 15:29
  • Using the absolute path, starting from catalina base: `private static String data_path = new File(System.getProperty("catalina.base")).getAbsolutePath()+"\\webapps\\myProjectName\\WEB-INF\\classes\\project\\fdsProject\\"` and so on. – Ildar Allayarov Apr 07 '17 at 15:38
  • Well there's your problem right there. – nitind Apr 07 '17 at 15:39
  • `new File(System.getProperty("catalina.base")).getAbsolutePath()` gives a path to the tomcat installation folder, where the webapps and my WAR file is located independing of my computer setting. Where exactly can be a problem? – Ildar Allayarov Apr 07 '17 at 15:46

2 Answers2

0
  1. Stop the Tomcat Server

  2. In the settings view Tomcat Server settings in Eclipse -> Server Locations, check Use Tomcat Installation(...)

  3. Now, you can change the Deploy Path, if you want webapps, then input it.

EDIT

The values below control how tomcat deal with war file

autoDeploy="true"
unpackWARs="true"

Just search these values in your server.xml file and edit values as you need.

Yu Jiaao
  • 4,444
  • 5
  • 44
  • 57
  • After adding new server Settings, near my Project appeared a new Project "Servers". Do I need to include it somehow to a WAR file of my project? – Ildar Allayarov Apr 07 '17 at 14:07
  • After editing and saving your Server, you can use `Run on Server ...` context menu to use it – Yu Jiaao Apr 07 '17 at 14:15
  • Well I tried as you said. As a result: 1) eclipse creates a new folder with my project name in a tomcat/webapps, when I "Run on Server" in Eclipse 2) I created WAR file from a project and deployed it on a Tomcat. 3) Tomcat created in tomcat/webapps near my project's folder a new folder based on a WAR and I could run the new added WAR application. 4) After that I decided to test it on another machine. I copied the project folder to a new location and deployed the war file. So I had the same files as on the first machine. And it didn't work! Without adding a project folder it doesnt work as well – Ildar Allayarov Apr 07 '17 at 14:34
  • Tomcat always unzip war to a directory with the name as the war file as a default work mode – Yu Jiaao Apr 07 '17 at 14:41
  • Yes, it does in my case as well. In tomcat/webapps I have "myProjectName", which was generated after I ran my project on a Server, the WAR file say "test.war" and a folder test. But the names of a war file and of my project are different. I think it does not make much sense. So I am confused now, how to run it on another machine that it really works just from a single WAR file. – Ildar Allayarov Apr 07 '17 at 14:47
  • Anyways it works fine with the wtpwebapps folder, so if I dont't find any solution for standalone WAR I will keep using this working solution. Thanks for your efforts. – Ildar Allayarov Apr 07 '17 at 15:00
0

If you want to be able to deploy your app as a WAR file, you need to refer to everything within it relative to the app itself, not with absolute file paths, not even as file paths if you can. If you have code that requires a java.io.File, rewrite it. The point is that you don't care where things are, you just care about how to get to their contents. For a Servlet, you can read anything on the web app's classpath by calling getResourceAsStream() on the correct class or classloader.

See getResourceAsStream() vs FileInputStream .

Community
  • 1
  • 1
nitind
  • 19,089
  • 4
  • 34
  • 43
  • thanks for the issues, you left me. I've read them and try to build my path now. But it seems that I am doing something wrong, as I get null as a result. I edited my question and put my project structure here. I am trying to reach the fdsProject folder, to get its path as a String and to submit it to a next variable. What I am doing is: `InputStream path = CBREngine.class.getClassLoader().getResourceAsStream("src/project/fdsProject/FDS.prj");` After that I need to convert InputStream to string i guess. – Ildar Allayarov Apr 07 '17 at 18:57
  • With `src` as an entry on the Java Build Path, it becomes an entry on the runtime CLASSPATH, so you don't use it as part of the relative reference. You're on the right track, though. Use a Reader to decode the InputStream into text. – nitind Apr 07 '17 at 19:01
  • Nice to hear that. I customised my code and tried to convert it to text as you wrote, but what I see are just unreadable symbols. I am probably missing something... My code looks like that:`InputStream path = CBREngine.class.getClassLoader().getResourceAsStream("/project/fdsProject/FDS.prj");Reader reader = new InputStreamReader(path); BufferedReader br = new BufferedReader(reader); String dataPath = null; while ((dataPath=br.readLine())!=null) { System.out.println("Data path: " +dataPath); }` – Ildar Allayarov Apr 07 '17 at 19:27
  • Sounds like either a file encoding problem, or the file doesn't contain what you think it does. Might be time for a new Question. – nitind Apr 07 '17 at 19:33
  • I actually just need a path to that file as a string, not to open the file. This will be done in the next step if I provide a right path to it. Is a parameter which I give to getResourceAsStream() function correct? Maybe the first slash before project folder is needless? – Ildar Allayarov Apr 07 '17 at 19:39
  • I reduced my source path to `InputStream path = CBREngine.class.getClassLoader().getResourceAsStream("/project/fdsProject");` and see the files in a folder, but not a full path to them. – Ildar Allayarov Apr 07 '17 at 19:50