• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Frits Walraven
Bartenders:
  • Carey Brown
  • salvin francis
  • Claude Moore

CWD Eclipse Java project with Tomcat 9 (absolute path to relative)  RSS feed

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone and thanks in advance. I am new here so I will try to help you and ask some doubts

I have been working with a Java project in local using Tomcat (The IDE is Eclipse) and now I want to migrate this to a server so I have to change the paths making it relatives because my paths are absolute.

And here I'm having a problem. When I run the project in Tomcat (localhost), the project can't find the CSV file.

The structure of the project is this:

|Project
| |-src
|   |-main
|     |-java
|     |-resources
|       |-file.csv
|     |-webapp

So what could be the problem? Maybe is the CWD? the CWD is from the tomcat server or is from the current workspace?

I have been trying differents solutions that I've found in Google but no one works for me.

Thank you again guys.
 
Saloon Keeper
Posts: 20635
122
Android Eclipse IDE Java Linux Redhat Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the JavaRanch, Jose!

Your problem is that you are walking around on crutches.

In the Real World, running web applications on production computers, there is no Eclipse available. Only Tomcat. So how you set up things in Eclipse does not count.

In order to run a real web application, you much create a Web Application Archive (WAR), which is a JAR file with a special kind of organization. You can then transfer that WAR to the Tomcat server for execution (deployment).

A WAR appears to Tomcat as a small self-contained filesystem. Since in its official form it's a ZIP file (although Tomcat will "explode" it into actual files and directories), the way to locate resources ("files") is to use the J2EE API.

You can find any resource in a WAR by using the webapp's ServletContext object. There are two methods: getResource() and getResourceAsStream().

Your Eclipse directory tree looks like it's set up as a Maven project. Maven can produce a WAR using that directory layout, and the resource path for your CSV file in that WAR would be "/file.csv" in that case.

There is no such thing as a Working Directory in a webapp. The Working Directory API methods in Java are not guaranteed to produce reliable results, so use the resource path, instead.
 
Jose Chovi
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tim Holloway wrote:...



Thanks a lot Tim

I've never made a back end project and then upload to use in a webpage ... for that reason I am having these problems.

The two methods that you are telling me is only possible use it in a servlet class or can I use it in a Java class?
For example, I have three servlets and three classes that are called by the servlets. In the servlets I use these classes so is this a problem? Do I Have to do all the code in a servlet class or can I have differents classes that are not a servlet?

If you know some web tutorial to do this I would appreciate it so much
Thank you again Tim for your help!



 
Tim Holloway
Saloon Keeper
Posts: 20635
122
Android Eclipse IDE Java Linux Redhat Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, the ServletContext does not have to be used only in a servlet class, and, in fact, a few days ago we discussed that with someone else here on the Ranch.

You have to obtain the ServletContext object for the webapp in the servlet's process() method or one of its derivatative methods (doGet, doPost, and so forth). But the processing method can then use a business logic bean to do heavy work (in fact, we recommend that). You just have to pass the ServletContext object you got to the business logic bean.
 
Now I am super curious what sports would be like if we allowed drugs and tiny ads.
Create Edit Print & Convert PDF Using Free API with Java
https://coderanch.com/wiki/703735/Create-Convert-PDF-Free-Spire
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!