aspose file tools*
The moose likes IDEs, Version Control and other tools and the fly likes Execution dir (in Eclipse) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Engineering » IDEs, Version Control and other tools
Bookmark "Execution dir (in Eclipse)" Watch "Execution dir (in Eclipse)" New topic
Author

Execution dir (in Eclipse)

Jan Hoppmann
Ranch Hand

Joined: Jul 19, 2010
Posts: 142

Hey guys!

At the moment, I'm creating an application using Vaadin and running it via the build in "Run as -> Run on Server" function of Eclipse.
So, when it runs, the directory in which it works is my Eclipse installation directory, but I need it to be my binaries folder - and that should work even when I'm no longer depending on Eclipse, but run it on an external Tomcat.
I tried googling for the answer, but didn't find any. Does anyone of you know where I can tell my code to find its binaries folder?


Life is full of choices. Sometimes you make the good ones, and sometimes you have to kill all the witnesses.
Wendy Gibbons
Bartender

Joined: Oct 21, 2008
Posts: 1106

when it runs through tomcat, the execution dir will be the tomcat directories (well it always was for me). You can change the paths where tomcat looks for stuff in one of it's many xml configuration files.

and I am presuming when you talk about binaries, you mean the .class files (which aren't actually binary)?
Jan Hoppmann
Ranch Hand

Joined: Jul 19, 2010
Posts: 142

Yeah, I meant that, sorry - I'm so used to call compiled code binaries, even though .class-files aren't.

Huh, okay, so I'll check the config files / ask Google how to config my Tomcat
Wendy Gibbons
Bartender

Joined: Oct 21, 2008
Posts: 1106

Jan Hoppmann wrote:Yeah, I meant that, sorry - I'm so used to call compiled code binaries, even though .class-files aren't.

Huh, okay, so I'll check the config files / ask Google how to config my Tomcat

fully understand I code in both C and java, get confused is it a method or a function, binary or class file etc.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15632
    
  15

Webapps should never assume that they have a "current directory" or an "execution directory". The J2EE standard doesn't guarantee it, and the JVM is fully within its rights to change current directories without telling anyone.

On top of that, the actual J2EE standard for webapps is a WAR, and a WAR is a ZIP file, so the resources within a WAR cannot be expected to actually exist as independent filesystem files and directories. If you configure Tomcat not to explode WARs, for example, they won't.

The "execution directory" for a Java app is the classpath. The rules for classpaths are defined by the J2EE standard and the architecture of the webapp server. If you're looking for the literal execution directory to do something like a Runtime.exec() out of, don't even consider that you might have a reliable "execution directory" - invoke the exec'ed program/script via its absolute filesystem path just like you would using cron.

To make an absolute filesystem path, you need to have a reliable base path. The best way to get that is to pass it to the webapp as an appserver-configurable resource. The second-best way is to hard-code it into a properties file deployed with the app. The second-worst way would be to hard-code it into program logic. And the worst way would be to assume you know where it' always going to be. Murphy is watching, you know!


Customer surveys are for companies who didn't pay proper attention to begin with.
Jan Hoppmann
Ranch Hand

Joined: Jul 19, 2010
Posts: 142

Yeah, well, that is exactly my problem. I have a properties file in my project with the path to my classfiles (I need that for a class loader), but I can't open the properties file because I can't find it because I am in a different directory
Wendy Gibbons
Bartender

Joined: Oct 21, 2008
Posts: 1106

if my memory serves me right tomcat pretty much expects the classfile to be under WEB-INF/classes (or in a war file at the same location) is there any reason they couldn't go there?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18118
    
    8

Jan Hoppmann wrote:Yeah, well, that is exactly my problem. I have a properties file in my project with the path to my classfiles (I need that for a class loader), but I can't open the properties file because I can't find it because I am in a different directory


To paraphrase what Tim said: Forget about directories already. You aren't in a directory. The properties file isn't in a directory.

Now, if your properties file is in the classpath (and you should arrange that it is) then this is how you access it:

No directories needed. Just make sure the properties file is at the root of one of the classpath entities (i.e. either /WEB-INF/classes or one of the jars in /WEB-INF/lib).
Jan Hoppmann
Ranch Hand

Joined: Jul 19, 2010
Posts: 142

Please ignore / delete this post...
Jan Hoppmann
Ranch Hand

Joined: Jul 19, 2010
Posts: 142

I tried to modify my method, first I used a File object and it didn't find the file, and the input stream doesn't work, either.
I placed the .properties-file in my WebContent/WEB-INF/classes dir, and my code looks like this:



When I run this, I get an NPE at Properties$LineReader.readLine.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15632
    
  15

Jan Hoppmann wrote:I tried to modify my method, first I used a File object and it didn't find the file, and the input stream doesn't work, either.
I placed the .properties-file in my WebContent/WEB-INF/classes dir, and my code looks like this:



When I run this, I get an NPE at Properties$LineReader.readLine.


Yes, that can be unreliable in a webapp. There is more than one classloader at work.

Try this, instead:


I removed the try/catch because unless you built the webapp wrong, a failure to locate the resource "isn't possible". Although a malformed properties file might throw a parsing exception.

If you implement this as a non-static method invoked by a servlet's init() method, the servletContext will be directly available, and you won't need to pass it in.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Execution dir (in Eclipse)
 
Similar Threads
Relative path in eclipse RCP
Packages
Eclipse
Eclipse: Where is plugins folder on Mac?
The compilation unit is not on the build path..