Relative Path not working in Vaadin Application - java

I have a Vaadin application in Eclipse running using Tomcat. I have an external jar file that requires access to a folder "/WordNet-JWI/3.0/dict/".
This works perfectly fine in a normal Java application, where the folder stays in the main project folder, but in a web application, it can't resolve this relative path and gives an error Dictionary directory does not exist: WordNet-JWI/3.0/dict
I am fairly new to Web applications and don't know in which folder to put the Wordnet-folder and how to make it available to the external jar for it to run properly.
PS - I put the external jar in /WEB-INF/lib and is working fine.

Based on your path the file should be located starting from the root of your partition.
For example if you are using windows it would be C:/WordNet-JWI/3.0/dict/ if you are running the webapp on C:
Mind that on unix the "user" running the Tomcat process must have r/w permission on that folder.
Regards.

Related

Access resource files like xml inside jar after deployed on pivotal cloud foundry

I created one java micro service using spring boot. The application requires some data from static xml files (kept together in a folder, lets name it X) in src/main/resources folder.(I am using STS IDE)
It is working fine when I run it in the IDE itself.
Now when I pack it in a jar and try running it using CLI (java -jar jarFileName.jar) it works and access the folder X from the target/classes folder.
Now my problem is, when I try to deploy this small app on Pivotal Cloud Foundry using either CLI or STS plugin, I have to give a path to the jar file so it uploads only the respective jar file and starts the container with the application. But the application cannot access folder X which contains its resources.
Though I confirmed that the jar file contains the folder X in BOOT-INF/classes but it tries to find the folder X on its own path - not inside itself.
Does anyone have some idea how can a jar file deployed on PCF can access its resources kept inside itself.
I tried using
InputStream is = this.getClass().getClassLoader().getResourceAsStream("X/abc.xml")
but it didn't work. It is unable to find the path during runtime.
why do not try Apache Abdera.
your XML is obtained from an API.
just another way, btw.
In your main class(usually Application.java) add #ImportResource annotation at class level.
Example: #ImportResource("classpath:myfile.xml")

Eclipse Java Servlet access to higher level directory

I've installed Eclipse with web development and during download have installed Tomcat7 to dir E:\Eclipse\tomcat7.
When in Eclipse and trying to test the program I'm getting a realPath of
C:/Users/user/Documents/eclipseJEEWorkspace/javaWebPages/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/org.example.web/WEB-INF/classes/
as my real path and not what I expect which is E:\Eclipse\tomcat7\webapps\org.example.web/WEB-INF/classes/
1) how do I get the real path of the servlet rather than the temporary path...
or am I missing something with how Eclipse uses the installed server? If this is the way then do I have to continuously create the WAR and fiddle around recreating/deleting all the time?
The reason for this is I'm also trying to get a JAVA Servlet getting access to a file in a path higher than webapps. I'm new to servlets....
My development includes third party software that all reference an individual file our.properties so changing the structure is unfortunately not a option.
My directory structure is:
/tomcat
/mycompany
/properties
our.properties //the file we want to access
/*otherfiles
/html
/*not used in this context but to show usage
/javascript
/*not used in this context but to show usage
/webapps
/org.example01.web
/META-INF
/WEB-INF
/org.example02.web
/META-INF
/WEB-INF
/org.example03.web
/META-INF
/WEB-INF
How do I tell my Servlet to access the our.properties file?
I've tried getPath, getResource but without getting the first bit to work, I doubt I'll be getting anywhere fast.
Thanks
When you run Tomcat under Eclipse, via the Tomcat plugin, the webapps path is changed. Eclipse uses .metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/ folder to deploy the project. That's not a temporary path, it's the actual path since your web application is deployed there.
If you run tomcat from command line, or as a service and deploy your web application, you'll see that the path of the servlet will be as you expected.
In order the change the default path to deploy for Eclipse, double click to your Tomcat under Servers view and modify Server locations section.

Website directory structure different from eclipse

I am coding a website using java servlets and am using eclipse and tomcat. When I test it using localhost, it works fine. But when I am deploying it on my actual website, the directory structure is messed up and the files are not called properly.
My eclipse directory structure on localhost is
Project Name
.src/packageName/java files
.WebContent/HTML files.
When I make a call from the html files, I use the relative location and tomcat automatically knows to look in the src/packageName folder. For example, from the /WebContent/login.html page makes a onClick call as follows,
. This will automatically trigger the java file in /src/packageName/welcome
When I am deploying it in my actual website, the WebContent/login.html is throwing an error WebContent/welcome file is not found. How do I tell my website to search in /src/packageName folder?
Hmm...have you been sure to package the application as a war for deployment.

Setting the working directory for an eclipse plug-in

In the Eclipse plugin I am developing, I am querying some Prolog files that are in my development directory. In addition, an initialization file located at the root of my working directory is automatically loaded by the Prolog engine I am using. All these files are resources of my plugin.
When executing my application as a java standalone the only thing I need to do is to set, in the Eclipse Run Configurations dialog, the working directory to my default output directory. Then when executing my application the Prolog engine starts at the directory where the initialization file is located, and it is able to locate and load the prolog files of the application when needed. This Prolog engine is started from Java using the JPL library.
My problem started when I wanted to build an Eclipse plug-in of my application. Apparently the default working directory for plugins cannot be changed in the Run Configurations dialog (at least that is the case in my Mac setting), otherwise an infinite list of exceptions appear when the plug-in is executed.
So what I would like to know is if there is another way to setup the working directory for a plugin, both during the development lifecycle (i.e., when I launch my plugin as an Eclipse Application) and in production (i.e., when the plugin is in the plugin directory).
Thanks !!
Working directory is set for the application not a plugin. I can imagine the mess if all plugins would try to set the working directory to their liking...
Files in a plugin can be found using the Bundle class which can be accessed using plugin activator. This will give you a URL that most likely points to a JAR entry. FileLocator class can be used to "convert" that URL to a file URL (file will be extracted from JAR).
URL url = Activator.getDefault().getBundle().getEntry("file path in JAR");
URL fileUrl = FileLocator.toFileURL(url);
File file = new File(url.getPath());
If the file MUST be in the working directory then you can copy it there.

Serve Modules without publishing not working in Helios

I have recently moved a webapp I have been developing to a new machine running 64bit Eclipse Helios (Service Release 2) and I am using Maven plugin M2Eclipse.
I have deployed on a local tomcat install through Eclipse and everything is ok (more or less), but I want to select the option "Serve Modules without publishing", but when I select this option I get errors:
log4j:ERROR Could not read configuration file from URL [file:/C:/butterfly/svn/trunk/micro/src/main/webapp/WEB-INF/classes/log4j.properties].
java.io.FileNotFoundException: C:\butterfly\svn\trunk\micro\src\main\webapp\WEB-INF\classes\log4j.properties (The system cannot find the file specified)
The log4j.properties file is not there, as in my source directories in lives in src/main/resources - at build it then gets copied over to target/WEB-INF/classes/..
Eclipse seems to be mixing the expected target directory with the src directory so not finding it.
Im not sure if this is happening for just the properties file or if the same problem will occur looking for all built resources.
I have seen these issues:
https://bugs.eclipse.org/bugs/show_bug.cgi?id=318449
http://www.eclipse.org/forums/index.php?t=msg&goto=661045&S=25bafd85b11e042c169ecf1752bfa479
but they seem to be slightly different or already fixed (My Helios is a new download from last weekend)
Anyone experience this or know how to resolve?
From here: "The Serve modules without publishing option does what it says. Web content will be served directly from the "WebContent" folder of the Dynamic Web Project. A customized context is used to make the project's dependencies available in the Web application's classloader". I would expect eclipse to emulate serving every class / resource file (including log4j.properties) from WEB-INF/classes after you build the project. As a workaround, what about creating a "classes" folder inside WebContent, copy log4j.properties file here and see if the classloader gets happy?

Categories

Resources