Tomcat将项目目录用于相对路径

时间:2012-06-06 09:47:25

标签: tomcat path jvm

我有一个GWT Web应用程序,它在相对目录“cache”下的磁盘上创建一个缓存。在我的eclipse中的JUnit测试中它可以工作,但是当我在Tomcat中部署它时,程序仍然尝试在/home/username/projects/myproject/cache(实际路径重命名)而不是/var/lib/tomcat7/webapps/myproject/cache中创建文件,因为它没有文件写访问权限,所以崩溃了。创建缓存的代码是外部库,因此甚至无法进行硬编码。

它可能是一个象征性的链接吗?或者Tomcat和Eclipse可以共享JVM吗? 这个错误让我发疯。

错误消息

org.h2.jdbc.JdbcSQLException: Fehler beim Erzeugen der Datei "/home/username/projects/myproject/cache"
Error while creating file "/home/username/projects/myproject/cache" [90062-143]
    at org.h2.message.DbException.getJdbcSQLException(DbException.java:327)
    at org.h2.message.DbException.get(DbException.java:167)
    at org.h2.message.DbException.get(DbException.java:144)
    at org.h2.store.fs.FileSystemDisk.createDirs(FileSystemDisk.java:368)
    at org.h2.store.FileLock.lockFile(FileLock.java:328)
    at org.h2.store.FileLock.lock(FileLock.java:134)
    at org.h2.engine.Database.open(Database.java:525)
    at org.h2.engine.Database.openDatabase(Database.java:208)
    at org.h2.engine.Database.<init>(Database.java:203)
    at org.h2.engine.Engine.openSession(Engine.java:53)
    at org.h2.engine.Engine.openSession(Engine.java:147)
    at org.h2.engine.Engine.createSessionAndValidate(Engine.java:126)
    at org.h2.engine.Engine.createSession(Engine.java:109)
    at org.h2.engine.Engine.createSession(Engine.java:27)
    at org.h2.engine.SessionRemote.connectEmbeddedOrServer(SessionRemote.java:289)
    at org.h2.engine.SessionRemote.createSession(SessionRemote.java:265)
    at org.h2.jdbc.JdbcConnection.<init>(JdbcConnection.java:110)
    at org.h2.jdbc.JdbcConnection.<init>(JdbcConnection.java:94)
    at org.h2.Driver.connect(Driver.java:73)
    at java.sql.DriverManager.getConnection(DriverManager.java:579)
    at java.sql.DriverManager.getConnection(DriverManager.java:221)

[...]

1 个答案:

答案 0 :(得分:1)

请尝试

String path = new File(servletContext.getRealPath("/cache")).getAbsolutePath()