問題描述
在程序文件夾中創建鎖定文件會導致異常 (Creating Lock file in Programs Folder causes exception)
我最近決定為我的 Java 應用程序使用官方安裝程序。
應用程序將自身安裝在 Programs Files 下的適當文件夾中。
在 bin 文件夾中我的應用程序所在的 jar 文件,我有一個 h2.db 文件,其中包含應用程序讀取的一堆信息。
當我嘗試在安裝位置運行應用程序時,我得到一個異常:
p>
org.h2.jdbc.JdbcSQLException: IO Exception: "java.io.FileNotFoundException: C:\Program Files (x86)\Aurora Game Hub\bin\AuroraDB.lock.db (Access is denied)"; "C:/Program Files (x86)/Aurora Game Hub/bin/AuroraDB.lock.db" [90031‑167]
at org.h2.message.DbException.getJdbcSQLException(DbException.java:329)
at org.h2.message.DbException.get(DbException.java:158)
at org.h2.message.DbException.convertIOException(DbException.java:315)
at org.h2.store.fs.FilePathDisk.newOutputStream(FilePathDisk.java:265)
at org.h2.store.fs.FileUtils.newOutputStream(FileUtils.java:223)
at org.h2.store.FileLock.save(FileLock.java:197)
at org.h2.store.FileLock.lockFile(FileLock.java:333)
at org.h2.store.FileLock.lock(FileLock.java:128)
at org.h2.engine.Database.open(Database.java:542)
at org.h2.engine.Database.openDatabase(Database.java:222)
at org.h2.engine.Database.<init>(Database.java:217)
at org.h2.engine.Engine.openSession(Engine.java:56)
at org.h2.engine.Engine.openSession(Engine.java:159)
at org.h2.engine.Engine.createSessionAndValidate(Engine.java:138)
at org.h2.engine.Engine.createSession(Engine.java:121)
at org.h2.engine.Engine.createSession(Engine.java:28)
at org.h2.engine.SessionRemote.connectEmbeddedOrServer(SessionRemote.java:305)
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:72)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at aurora.engine.V1.Logic.ASimpleDB.searchAprox(ASimpleDB.java:828)
at aurora.V1.core.GameSearch.searchGame(GameSearch.java:249)
at aurora.V1.core.GameSearch.run(GameSearch.java:346)
at java.lang.Thread.run(Unknown Source)
Caused by: java.io.FileNotFoundException: C:\Program Files (x86)\Aurora Game Hub\bin\AuroraDB.lock.db (Access is denied)
at java.io.FileOutputStream.open(Native Method)
at java.io.FileOutputStream.<init>(Unknown Source)
at java.io.FileOutputStream.<init>(Unknown Source)
at org.h2.store.fs.FilePathDisk.newOutputStream(FilePathDisk.java:257)
... 22 more
當它不在程序文件位置時它工作正常,因為我在開發時會注意到這一點。
我認為它與權限有關,而不是鎖定文件能夠被創造什麼的。有沒有辦法給予/請求明確的權限來創建鎖定文件,或者我可以告訴 H2 不創建鎖定文件?<
參考解法
方法 1:
It seems there are no access rights to write to this directory. You need to store the database file in a directory where you do have access rights.
For H2, if you use the database URL jdbc:h2:~/data/db
, the database is stored relative to the current user home directory. Another alternative is to use an absolute path, for example jdbc:h2:c:/dir/to/db/file
方法 2:
This is caused by improved security in windows. You can write only when you have elevated privilages. For example if you start program as 'run as administrator', then it may able to write to Program Files area. Then it is not a good idea‑ see: Bypass Windows permission restrictions on program files folder
You should use folder pointed by 'ProgramData' variable. or good old user.home area
方法 3:
On Mac OS X, you can use the known subdirectories of user.home
cited here.
Java Web Start with suitable permissions may be an alternative, although I haven't tried it.
(by Sammy Guergachi、Thomas Mueller、Jayan、trashgod)