1

Quick question:

For some initiations I need to pass java URL class with url to specific file. But I posses this file in resources in my aplication. So I ask is there possible to create java URL class with relative address? I have never seen something like that, so I wonder.

0

2 Answers 2

1

Try creating a File object first (where you can provide a relative path to your resource) and then convert it to an URL:

File file = new File("resources/myresource.abc");
URL url = file.toUrl();
Sign up to request clarification or add additional context in comments.

Comments

1

You can get a URL relative to where the your java class file is and refernce files in the same location. This is done using the the getResource() method of class. Place the resource in a folder called "resources" in the same position as your .class file. Then you can get a url relative to the position of the class like this:

URL url = <InsertYourClassName>.class.getResource("resources/<InsertNameOfResource>");

This will get a url path relative to where you class is and look in the resources folder for your resource. The same can be achieved by just placing the resource in the same directory as your class but not in a resources folder like this:

URL url = <InsertYourClassName>.class.getResource("<InsertNameOfResource>");

Hope this helps :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.