• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Execution dir (in Eclipse)

 
Jan Hoppmann
Ranch Hand
Posts: 147
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Wendy Gibbons
Bartender
Posts: 1110
Eclipse IDE Oracle VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 147
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1110
Eclipse IDE Oracle VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 18098
50
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Jan Hoppmann
Ranch Hand
Posts: 147
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1110
Eclipse IDE Oracle VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20976
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 147
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please ignore / delete this post...
 
Jan Hoppmann
Ranch Hand
Posts: 147
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 18098
50
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic