Opening file in current directory in java with file protocol

时间:2015-10-29 11:07:55

标签: java url

I have a license agreement file that needs to be open in default browser. The file lies in installation folder itself. I am doing it in java with awt as like this which is working fine:

Desktop d=Desktop.getDesktop();
 d.browse(new URI("file://D:/OMS-Install/OMS/oms_license.txt")); 

But since the entire folder can be placed anywhere on windows drive, at run time I need to consider the current directory. How can I achive this with Java & Default browser of AWT.

Doing it as there is a requirement. I would have otherwise followed many other options to accept terms and conditions.

Edit Adding working code:

    String path=new File("OMS/oms_license.txt").getAbsolutePath();
    File license=new File(path);
    URI urlLicense = license.toURI();           
    d.browse(urlLicense);

2 个答案:

答案 0 :(得分:1)

您可以使用Class.getResource()检索类路径上某些内容的URL。

有些东西 URL license = getClass().getResource("/OMS/license.txt");

答案 1 :(得分:0)

您可以转换当前目录中的get文件并计算其绝对路径

new File("./OMS/oms_license.txt").getAbsolutePath()