Matt
Matt

Reputation: 181

How to get Java to look at a specific location for a file

I'm creating a Jar file which automatically creates a target folder.

Within the the target folder I have an XSD file. When I execute the jar file via the command prompt - it is looking for the file in the target folder as opposed to the test-classes folder.

Is there anyway I can configure this so that the compiler looks at the test-classes folder for the file?

Thanks

Upvotes: 1

Views: 69

Answers (1)

Timothy Truckle
Timothy Truckle

Reputation: 15634

The "test-classes folder" is just a random place on your hard disk.

Java supports 3 generic places to look for resources:

  1. the current working directory. this is where your command prompt is.

  2. the user home This is what you get when calling System.getProperty("user.home"). On Unix this resoves to $HOME and on Windows to %USERPROFILE%

  3. the own code location. Resources in the same package as your class are accessed with getClass().getResource("filenameWithoutPath") But usually you place resources in a special folder in the application root and access it like this: getClass().getResource("/relative/path/from/src/root/filenameWithoutPath")

Upvotes: 3

Related Questions