Eclipse, Tomcat project context root is eclipse folder - java

I'm having a little problem configuring my Tomcat Project. In short, I'm working on a Dynamic Web Page project but there seems to be some confusion as to from where I am running the application.
Running the following code
File f = new File("");
System.err.println(f.getAbsolutePath());
yields the output C:/Eclipse which happens to be where I run Eclipse.
Normally I'd expect this to be the output catalog, but it is not the case this time.
My eclipse project is configured to have myProject/WebContent/WEB-INF/classes as output folder
How can I configure to the desired directory? Right now I need to use absolute paths which won't work for all members of the project.

Alright, I found a solution.
It seems my default working directory was set to my eclipse folder for the project so i solved it by specifying the project root folder instead which normally is the case.
I configured this here:
Run configurations > Apache Tomcat Run configuration > Arguments tab, under "working directory" choose "other" and specify directory.
Thanks for your comments!

Related

Rest service in Java runs from Tomcat server but not inside Eclipse

I am unable to find another article that solves my problem but am happy to hear about one if you know the answer.
I have a RESTful service built in Java with Eclipse. It uses Spring and all of its bells and whistles.
After much work with configuration files, I am able to build the service into a war file, deploy it to my Tomcat webapps folder, and run it from standalone Tomcat. However, it still throws several errors on start when I try to start it from a service defined within Eclipse. The errors are the same errors that I was seeing before I got the config files correct for standalone Tomcat. The errors are about the inability to create the required beans because of references to properties that can't be found.
How did I get it running in Tomcat? I added a couple of .properties files to define properties needed by the beans that get launched at startup and then added a 'set CLASSPATH=...' line to the setenv.bat file in the Tomcat bin folder. This new line adds the properties files into the CLASSPATH. That seems to have fixed everything from standalone Tomcat.
So, my question is, "How do I make these same changes inside my Eclipse server?" I have added the properties folder with the property files under the config folder in Eclipse, just like it is in my native Tomcat folders, but I do not know how to modify the CLASSPATH string to specifically point to my .properties files (as I have done in the setenv.bat file for standalone Tomcat).
I think I understand the problem, but have no idea about the solution.
Thanks for listening.
Dave
Eclipse does not use any *.sh/*.bat files to startup Tomcat. In order to modify the way the server is started you have to open the configuration UI of the server by double-clicking on the server in the Servers view.
From there you will be able to modify the classpath of the system classloader through "Open launch configuration".
Another important setting is "Server path" which tells Eclipse, the value of $CATALINA_BASE. You can modify it only after removing all modules and cleaning the server. It is useful to set it to an easily accessible directory: this way you can verify directly that Eclipse didn't mess up your application deployment (sometimes it "forgets" to copy some libraries).
The "Configuration path" setting tells Eclipse where to find the files (but not subdirectories) that will be copied into $CATALINA_BASE/conf.
Remark: if your application requires you to add libraries to the top classloader, there is probably a problem in your project. The "missing" libraries should be added to WEB-INF/lib of your application instead: look into the "Deployment assembly" of your Eclipse project configuration.

Java Web Project not building correctly

I've got a Java Web Project which I'm deploying to a Tomcat embedded in Eclipse's Servers Plugin, alas, when I navigate to the location of that webapp after publishing/starting the server, the .class files are all missing. I can navigate as far as ProjectName\WEB-INF\classes\path\to\packages\ -> All packages are empty.
Any Idea how to solve this problem?
Edit
Build Automatically is checked and it compiles just fine for another web project I've just included, just not for the one specified
Edit 2
Reinstalled Eclipse (because why not) - nothing's changed, works for Project #2, doesn't work for Project #1. Not even the path to the packages is existant now. The content of the src folder (that's supposed to be moved to WEB-INF/classes) isn't there at all - but the resources (I've got a resources folder with the same deployment assembly command) are.
A few steps to perform to restore sanity...
Refresh your project. It is possible that something was modified on the file system external to Eclipse.
Kick of Project -> Clean.
Now look at your project (not the deployed location) on the file system. Do you see the expected files in the /bin directory (or whatever you may have changed class output directory to)?
Let's now try exporting a war using the wizard. File -> Export -> Web -> WAR or something like that. Check the WAR contents. Are you .class files there?
If everything is good so far, there is nothing wrong with your project. The Tomcat server adapter just got confused.
In the Servers view... Make sure Tomcat is running. Next right click on Tomcat and select add/remove projects. Remove your project. Confirm that your web app is no longer accessible.
Now re-add your project via add/remove projects or invoke one of the run as actions.
The above should reset Tomcat server adapter and hopefully things should work as expected.
Check if you had deleted or moved your external JARs associated with the project on your IDE. I had moved it and ran into the same issue.

How can I tell from a JUnit test whether an Eclipse project is open?

I need to run Maven from the command line while Eclipse is open. Since this can confuse Eclipse, I configured the two to use different output folders (Maven writes to target, Eclipse m2e uses target-eclipse). This works very well.
But in one of my unit tests, I need to load classes which are part of a project but not on the classpath. Basically, I build my own URL classloader. Now, I have a problem: Which class folder to add?
If the project is open, target-eclipse/classes is the correct choice, otherwise I should use target/classes.
Assume that I know the Eclipse folder and the workspace folder. How can I tell from Java code running in a child process of Eclipse (not an Eclipse plugin) which projects are open and which aren't?
The existence of the folder is not a good criteria since the folder isn't deleted when I close a project.
[EDIT] Basically my question is: Eclipse must store that information somewhere in $workspace/.metadata or in $ECLIPSE_HOME/configuration. In which file do I need to look?
A file [Workspace_Home]/.metadata/.lock tells You the workspace is open.
The list and settings of projects in the workspace are in [Workspace_Home]/.metadata/.plugins/org.eclipse.core.resources/.projects.
It seems like open Java projects have a folder [Workspace_Home]/.metadata/.plugins/org.eclipse.core.resources/.projects/[Project_name]/.indexes, and closed don't have it. - I double checked it, and it seems I was wrong here.
But I just checked that closed Java projects have a file: [Workspace_Home]/.metadata/.plugins/org.eclipse.core.resources/.projects/[Project_name]/org.eclipse.jdt.core/state.dat, and open projects don't have it.
I would just use some configuration parameter (a system property for example), telling where the folder is. See http://maven.apache.org/plugins/maven-surefire-plugin/examples/system-properties.html for how to pass system properties when running tests with Maven. The first example in this page shows how to pass the build directory as a system property.

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?

When does Eclipse copy resources to the output folder?

I noticed that "sometimes" the resources i put into a source folder in my Eclipse project will not be copied to the output folder ("bin") immediately.
E.g. i change a properties file using the eclipse editor and save... and "bin" still has the old version.
Does anyone know what exactly triggers the copying (and how i can trigger that from a plugin)?
I thought it happened automatically when a resource changes.
-- EDIT --
To clarify what i meant by triggering it from a plugin: I have a plugin that depends on up to date resources in the output folder, but apparently the resources are outdated when my plugin is called. And that's why i need to better understand what happens and how i can force it when i need to.
If you don't add the resource through eclipse (but for instance through the windows explorer), eclipse wont notice some changes until you refresh your project/folder. (right click in the packater explorer for instance).
After it notices the change it will copy it to the output folder when you build (often this is done automatically upon changes)
This is a Bug in almost all Eclipse versions, it happens without any reason. My eclipse 3.5.2 and 3.6.2 both meet this problem in someday. In my case, I need to copy modified .clj clojure source files to classes directory for immediate effection in a web app, now I have to set /src before /classes in CLASSPATH of web appserver startup script, It's solve my problem temporarily.
As #Thirler said, if you don't add the resource through Eclipse, it may not notice the files until you refresh your project. However, if the Refresh/Clean doesn't work (in my case, it didn't), you have to add the files to the folder structure IN ECLIPSE.
Just drag the file from the folder where it is in the file system using Finder/Windows Explorer, drop it into the same folder in Eclipse's Project Explorer (Eclipse will ask for overwriting, just say yes) and Voila!
EDIT:
An easier way is to just navigate into the folder using Project Explorer and then Refresh (F5).
As far as I know it happens when you build the project. You don't need a plug-in, you just need to tell eclipse to do a build of the project.
There is an Eclipse setting Java > Compiler > Building > Output folder > Filtered resources.
For unknown reasons, this was set to *.launch,*.testsuite,*.deploy,*.location,*.execution,*.datapool,*.artifact,*.html,*.svg in my workspace (while the default seems to be just *.launch). This was the reason, why my Eclipse constantly refused to copy a classpath resource index.html.
Late response, but I just stumbled over this question but non of the answers helped me to convince Eclipse to do its job.
This happened to me when I imported an Eclipse project as a whole, including both src and bin directories. The fix is to Clean the project: Project -> Clean... -> select your project and press Clean. This should re-build the project and properly locate the resources needed.

Categories

Resources