简体   繁体   中英

How Can I Return the Executable's .java file path?

I want to read a number of images which are on a folder called images at the same folder with my source code files. The path I use to read each image, is ..\\images\\imageX.jpg . But it does not recognize it. I am using Netbeans.

This will get you the path to the src/ directory in your web application:

String path = YourClass.class.getProtectionDomain().getCodeSource().getLocation().getPath();

You can then append anything you need to navigate to your images directory. So assuming that the images directory were located in a folder inside the src directory, the following should do the trick:

File imageFile = new File(path + "images/imageX.jpg");

Keep in mind that getPath() will return with a trailing forward slash at the end, so you don't need to include one when further resolving the path to your files.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM