在程序文件夾中創建鎖定文件會導致異常 (Creating Lock file in Programs Folder causes exception)


問題描述

在程序文件夾中創建鎖定文件會導致異常 (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 GuergachiThomas MuellerJayantrashgod)

參考文件

  1. Creating Lock file in Programs Folder causes exception (CC BY‑SA 2.5/3.0/4.0)

#locking #java #h2 #file-permissions






相關問題

C# / ASP.NET - Web 應用程序鎖定 (C# / ASP.NET - Web Application locking)

在程序文件夾中創建鎖定文件會導致異常 (Creating Lock file in Programs Folder causes exception)

我什麼時候會使用 AutoResetEvent 和 ManualResetEvent 而不是 Monitor.Wait()/Monitor.Pulse()? (When would I use AutoResetEvent and ManualResetEvent instead of Monitor.Wait()/Monitor.Pulse()?)

鎖定一個 JavaScript 函數 (Lock a JavaScript Function)

當只有一個線程寫入共享變量時,我需要鎖嗎? (Do I need a lock when only a single thread writes to a shared variable?)

為什麼 lock(objLock) 比 lock(this) 好 (Why is it better to lock(objLock) than lock(this))

雙重檢查鎖定的修復有什麼問題? (What's wrong with this fix for double checked locking?)

在表格行上調用 Dibs (Calling Dibs on a table row)

我怎樣才能優雅地寫 lock {}? (how can I write lock {} elegantly?)

Double Check Lock 不能在這個 java 代碼上工作? (Doubly Check Lock Dosen't work on this java code?)

LINQPad / LINQ To SQL - 簡單查詢僅在循環內執行時才會引發內存不足 (LINQPad / LINQ To SQL - Simple Query Throws Out of Memory Only When Executed Inside a Loop)

如何在 dynamoDB 中實現 50 次寫入的事務? (How can I implement a transaction of 50 writes in dynamoDB?)







留言討論