MKay
MKay

Reputation: 836

Opening file in current directory in java with file protocol

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);

Upvotes: 1

Views: 1323

Answers (2)

Nyavro
Nyavro

Reputation: 8866

You can convert get file in current directory and calculate its absolute path

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

Upvotes: 0

Kayaman
Kayaman

Reputation: 73578

You can use Class.getResource() to retrieve the URL of something on the classpath.

Something along the lines of URL license = getClass().getResource("/OMS/license.txt");

Upvotes: 1

Related Questions