datetesting.java is a java source file which is not available after compilation to bytecode. the source directory it was in is not available, too.
dir2 is the
file of the directory you execute the
.class file in. it seams that this happens to be
d:\workspace\project1 but you can't rely on this.
your dir2 points to working directory (
new file(".")). you can't get the location of your sources this way. your file could sit inside the package (e.g.
your.company.date.datetesting). you should just manually concat the "src" to current working directory and then replace file package dots (
file.pathseparator. in that way you will build the full path to your file.
string fullfilepath = "h:\\shared\\testing\\abcd.bmp";
file file = new file(fullfilepath);
string filepath = file.getabsolutepath().substring(0,fullfilepath.lastindexof(file.separator));
if you are doing this to try to read a file from the classpath, then check out this answer: how to really read text file from classpath in java
essentially you can do this
inputstream in = this.getclass().getclassloader()
otherwise, if you have some other requirement, one option is to pass through the src directory as a jvm arg when the application begins and then just read it back.
/** the actual file running */
public static final file jar_file = new file(myclass.class.getprotectiondomain().getcodesource().getlocation().getpath();
/** the path to the main folder where the file .jar is run */
public static final string base_directory = (jar_file != null ? jar_file.getabsolutepath().replace(jar_file.getname(), "") : "notfound");
this will work for you both in normal java execution and jar execution. this is the solution i am using in my project.
- how to get full path of the current file including the src file in java
- How to quickly copy the current editing file name or full file path in Eclipse (Luna)?
- How to get the current project path from eclipse using java
- How to get the Path of current selected file in Eclipse?
- How to get the full class name of the of an Java file in Eclipse?
- How get the path of a java file selected in the pacake explorer in eclipse
- How to locate the Path of the current project directory in Java (IDE)?
- How do I get the file path of the open workspace for developing plugins in eclipse
- How to get the current time in your timezone in Java
- How to get full file path present in eclipse project explorer?
- How to get the part of xml file in JAVA using JAXB as a string
- Java read config file, how to get the right path in a clean way
- How to get full path of the selected node in the package explorer from an Eclipse plugin?
- How do I get my Java program to read the text file I want?
- How to get the absolute path of a file in my project (and not of a file stored in a temporary folder)
- How can I get the absolute path of the preference file for a particular plugin in eclipse plugin development?
- how can i get the path of a file in the project explorer of exlipse
- Trying to use Java file.getAbsolutePath() to get the absolute path of a file
- How do I get the gradle eclipse plugin to build a classpath including my additional java test task dependencies
- How to get path of java files including /src/
- How to get the path of an mp3 file from sd card on selecting the filename from playlist?
- How do I get the path of a file in an eclipse project?
- I want to get full path when I'm accessing a file stored in .jar file. How to do this?
- How to get current class name including package name in Java?
- How can I get relative path of the folders in my android project?
- How can I set the current line of execution in the eclipse java debugger?
- How do I add a shortcut key to Eclipse 3.2 Java plug-in to build the current project?
- Get the absolute path of the currently edited file in Eclipse
- how to add an xml file to the java application's classpath in eclipse?
- Eclipse Plugin: how to get the path to the currently selected project
More Query from same tag
- How to change the color of the currently selected line in a code assist popup in Eclipse?
- Java (Eclipse) reference to my own jar library won't resolve
- Deleteing entites/objects from the "Data store" in eclipse
- Android widgets properties popups not showing in Eclipse on Mac OS Lion
- eclipse + multiple checkouts of version-controlled projects
- how to install jdk 7 in eclipse
- Git migration? oldEclipseVersionOfProject -> newAndroidStudioVersionOfProject
- Integrating jprofiler into eclipse
- How to read and write text files from appengine?
- debug GWT on tomcat using eclipse
- Can't find maven eclipse builder
- user.dir is incorrect while using tomcat web app in eclipse
- Eclipse: File matching strategy being called on single click of any view
- NDK build error after replacing Tegra Android Development Pack by ndk-r9c
- How to set different simulator in BlackBerry - Java Plug-in for Eclipse?
- Tomcat 7 won't start on eclipse Kepler
- Error "Source option 5 is no longer supported. Use 6 or later" on Maven compile
- eclipse+pydev how to rename module?
- Eclipse Makefile project to be built within a Cygwin environment
- How to make my program take in any number of arrays rather than a set number?
- Maven is suddenly throwing error, Truncated class file
- How to add unit tests in Eclipse for a managed ARM C project?
- Eclipse IDE: Vertical text
- Disable Sonarlint automatic scan for Eclipse Remote Systems Explorer
- How to check out the values of dynamic allocated memory in debugging mode?
- What is the keyboard shortcut to go to a file in Eclipse on Mac OS X?
- Modifications in index.jsp doesn't appear when running the project
- Eclipse create android application
- Windowed Application not opening in eclipse
- Realm database integration with Eclipse Android project