Start my application from my home folder (relative path)

This is a 100% win console application. So here's the problem.

I want to load the file music.xm that I want to place inside the jar. The problem come up when I try to call the file through a relative path. The start directory it's not the Java project one, but my Windows User Folder.

If I call

File music = new File("\\music.xm");


javax.sound.sampled.UnsupportedAudioFileException: /C:/Users/XXXX/Desktop/./music/music.xm

If I call

    File music = new File(".\\music.xm");

I get

    javax.sound.sampled.UnsupportedAudioFileException: /C:/music.xm

Upvotes: 0

Views: 856

Answers (1)

rocketboy
rocketboy

Reputation: 9741

If its in your jar, you can use

getclassLoader().getResourceAsStream("music.xm")

You can use this inputStream however you like. But remember, the path should be relative to classpath root of the classloader.

In addition, if you are sure "music.xm" exists as an independent file on filesystem in a fixed relative location to your .class files you can also use :

getclassLoader().getResource("music.xm")

You can look on SO and here for documentation.

Upvotes: 1

Related Questions