Setting the working directory for an eclipse plug-in - java

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.

Related

(Tomcat 7) Deployed jar not finding files

I deployed a war file onto a Tomcat 7 instance running on a remote Linux machine and I'm getting FileNotFoundExceptions.
One of the referenced jars in the project, which contains code that I did not write, uses several files (which I have included, but it is not finding). These files are located in the classes folder. It appears the classpath I have set for the project is being ignored by this jar. These files that it uses, e.g. .properties files are external to the jar.
Here is an example of how it is invoking the files:
FileOutputStream fos = new FileOutputStream("Key.ser");
I was getting these errors when developing the source project in Eclipse. I was able to configure the project to tell it where to find these files via Run Configurations -> Arguments -> Other but the exported .war file appears to not have this bundled with it, only the source project has it. Now I'm seeing them again when trying to deploy the application to Tomcat on another server via war file.
How do I configure the deployed jar file in the deployed Tomcat 7 webapp to find these files that the jar uses? I am loathe to change the code since I did not write it so am really hoping to avoid this.
I am able to get this to work on a local Tomcat 7 running on Windows instance integrated with Eclipse as explained earlier so I'm wondering if maybe this can be duplicated?
You will not be able to find the file by simply referencing the file name using FileOutputStream. You are correct to place the file in the 'WEB-INF/classes' directory, which will allow it to be located on the classpath.
To load the file, you need to load it as a classpath resource using something similar to this:
String classpathLocation = ""Key.ser"";
URL classpathResource = Thread.currentThread().getContextClassLoader().getResource(classpathLocation);
// Or if you want it as an inputstream:
InputStream input = Thread.currentThread().getContextClassLoader().getResourceAsStream(classpathLocation);

class not found exception in eclipse even though the user library containg jar file is in java build path

I have created a dynamic web project. A user library with a jar file is also created.
Then the user library has been added to the build path via
project properties--->java build path--->In libraries tab required user library has added and jar file is specified under it.
But exporting the project into the JBoss deployment directory is raising class not found exception over the class in added jar file.
How can I properly add a user library to my project's build path?
I'm not clearly understand your problem but there is nothing to discuss ;)
Because the Java EE Specifications and the application server are dictating how to place something in your app's classpath and that's it.
Here is the documentation:
https://docs.jboss.org/author/display/AS71/Class+Loading+in+AS7
If your problem is not regarding the result on the appserver, only something about comfor or project strcture you want in your IDE. Then use your build system (Maven) to do the job. Like bilding a jar and coping it to desired location etc.

Relative Path not working in Vaadin Application

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.

New File gets created inside Eclipse Directory and not under Current Directory

I am trying to create a new File in Eclipse using the following Java code :-
File myFile = new File("sampleFile.txt");
myFile.createNewFile();
System.out.println(myFile.getCanonicalPath());
I printed the Canonical Path since I was not getting the file anywhere in my Project. Not in the context root (Its a code inside my Dynamic Web Project). Not in the src directory.
And I get the following output :-
E:\Softwares and Executables\eclipse for Java EE\sampleFile.txt
This is created inside the directory in which Eclipse is present. However, my workspace is following :-
D:\Code N Code\Practising Java\Eclipse Workspace\Web Workspace
What is the problem?
The problem is you're giving a relative filename - which means it's relative to the working directory of your process. You can set the working directory in the "Arguments" tab of the run configuration in Eclipse.
The default for running Java applications is within your workspace directory, but it sounds like Dynamic Web Projects work differently - I haven't used a DWP myself, but normally for things like web projects, the container you're running in gives you appropriate paths to create files relative to.

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