2012-06-06 87 views
0

我有一個GWT Web應用程序,它在相對目錄「cache」下的磁盤上創建一個緩存。在eclipse中的JUnit測試中,它可以工作,但是當我將它部署到Tomcat中時,程序仍然嘗試在/home/username/projects/myproject/cache(實際路徑重命名)而不是/var/lib/tomcat7/webapps/myproject/cache中創建文件,並且因爲它沒有文件寫入權限, 。創建緩存的代碼是外部庫,所以它甚至不能被硬編碼。Tomcat使用相對路徑的項目目錄

它可能是一個符號鏈接?或者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

請嘗試

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

感謝那就是繞過這個問題的好辦法,但它爲什麼會出現在第一名? –

+1

因爲它是這樣硬編碼的:http://code.google.com/p/h2database/source/browse/trunk/h2/src/main/org/h2/store/fs/FileSystemDisk.java?r = 3324# 59 –

+1

可能的設置-Duser.home = /緩存將幫助... –