I am stuck on a how to robustly find a file path from my Java program. If I am using Linux, I can launch my program from my home folder, and then I can't say find my file at ./myProgram/myFile. Is there a good way to find my file no matter what directory my console is in?
If you are trying to access the file using the path:
./myProgram/myFile
in your program, but you aren't executing the program from the myProgram directory, then you're Java code won't see the file. Try providing it with the full path instead of the relative path. If myProgram is a directory found in your user's Documents directory then a full path would likely look something like this:
/home/*username*/Documents/myProgram/myFile
You could also build in functionality that lets you select the file by navigating through the directories and listing the files. The would provide the user with options to choose which file to utilize in the program.
You can just as well cd to the myProgram directory before executing the file and then the relative path ./myProgram/myFile should work.
Hope that helps.
Try this:
File f = new File(System.getProperty("user.home") + System.getProperty("file.separator") + "myFile.txt");
Related
I have a text file ,i want to perform Automatic Term Recognition by using jajatr library. How can i set path of my file in this library. This library will process the text file and generate output file which will contain results.
I have downloaded this library from url provided below
< https://code.google.com/p/jajatr/downloads/list >
I have studied and found this thing helpful
After unzip the download folder . Traverse to the folder provided below
jajatr\jajatr\src\jatr\src
A Property File will be found named as jatr
Now set the text file path in the file named as jatr.
One of the statement in this file is :
jatr.system.refcorpus=/mnt/minerva1/nlp/corpora/monolingual/english/gigaword/gw.lemmas.unigram_counts
I think so that i have to set corpus path in this jatr.properties file
But I don't know how to set path of my corpus.
After running TestCValue.java class
Output
Usage: java TestTfIdf [path_to_corpus]
You do not need to change jatr.properties file inside the src directory of the jar file. You can rather supply the property you are interested to customize when you run the jar through the command line, something like:
java -jar jajatr.jar -Djatr.system.refcorpus=%YOUR-PATH%
I am using the below code to access and run a bat file
Runtime runtime = Runtime.getRuntime();
Process p =null;
p = runtime.exec("cmd /c c:/{foldername}/codereview.bat");
I realized that my code is not portable as i have hardcoded the path, so i copied my bat file in a folder under /src and am now trying to use it after removing the c:/{foldername} part but
the code is not working for me... The file can not be found.
Please help as to how i can provide the relative path inside the bat file.
I did not get your exact problem but I think this may help you:
To get the absolute path of your source program(the class file executing) use this:
String path = System.getProperty("user.dir");
Now you can dynamically the path of your program and the hence the batch file(which I think is relative to the class file)
so I have this problem:
I need to know where is Java saving the files when you create them with new, like this File file = new File ("file.txt"); on Linux?
Linux on not File file = new File ("file.txt") does not create a file on the file system. File is just a file path holder. You need to call file.createNewFile to create a file. Relative paths like file.txt are resolved against the current user directory, typically the directory in which the Java virtual machine was invoked
Assuming the file is actually being created by additional code (as Evgeniy mentioned), you could try checking to see if your current working directory isn't what you expect it to be. To find that directory you could try:
String cwd = System.getProperty("user.dir"));
System.out.println("Current working directory: " + cwd);
or just
System.out.println ("Path to file: " + file.getAbsolutePath());
To see where it should end up.
same place like in Windows: in the current directory
Your mentioned code doesn't create new file physically on drive, but logically yes. If you alter the code to create a new file like file.createNewFile then the directory where you executed the code, a file would be created there.
I have a small problem calling a path(that has the python file, that I need to run) in the following code:
Process p = Runtime.getRuntime().exec(callAndArgs,env,
new java.io.File("C:\\Users\\Balkishore\\Documents\\NetBeansProjects\\Testinstrument_Rest\\build\\web"));//excuting python file
As it can be seen from the above code, the python file is called using the path specified in java.io.file function. But it is very specific, as it can be run only in my computer. How can i make it generic, so that it is possible to run this piece of code in any computer?
Any help would be very much appreciated.
Put your python script to the location relative to your working directory and use relative path. Alternatively use configuration file or property to read the path from.
If this file is already exist in the app then you need to do
ServletContext.getRealPath("/");
which will give you the path to web root now from here you need to move relatively to reach to your file
If this is an external file
put it in ${user.home}/appname/
String filePath = System.getProperty("user.home")+File.separator+"APP_NAME"
and instruct your users to put the file in this path, or read the path from some configuration file (.properties, .conf)
Is there a way to read a file from a network shared location on windows?
Let's say for instance I've this simple code that reads a text file called readMe.txt from the Addons folder.
import java.io.File;
class Sample{
public static void main(String[] ar){
File file = new File("Addons/ReadMe.txt");
System.out.println(file.getAbsolutePath());
//followed by printing the contents of file
}
}
And I execute this file using a windows batch runme.bat that has
java Sample
PAUSE
The bat runs and executes the above class only when I place the Addons folder with ReadMe.txt, Sample.class, runme.bat file in my local drive.
When it is placed in a network shared location with UNC path like \\name\Shared
In such a scenario, the bat file typically starts the base from C:\Windows and throws a classNotFoundException. I can rather map the shared drive to a *Z:* drive or whatever but I do not want to do it.
I want the code to programatically detect and retrieve the content of Readme.txt in Addons folder irrespective of whether it is being executed on a local drive or on a shared drive. Is there a way to achieve this? Please help..
Thanks
Veekay
When using a file path in Java, make sure to escape all \ correctly when giving the full path name.
For example, if the file is on PC with IP (10.10.10.123) on a Shared folder called Addons then the full path will be:
File f = new File ("\\\\10.10.10.123\\Addons\\readme.txt");
Other than the full path, your code is throwing a ClassNotFound because you JAVA-CLASSPATH is not set properly.
In your bat file %~dp0 expands to the location of the bat file. You need that in your classpath so that java can find the class, though I don't know if it will choke on UNC path. For example:
#echo off
echo %~dp0
would output
\\host\share\dir
EDIT: %dp0 will not work if there are spaces. This is what you need in your bat file:
#echo off
set p=%~dps0
echo %p%
java -classpath %p%\jarname classname
pause
Two ways of doing.
1) Map the shared path to a local drive.
2) Else hard code the server path in new File('') as mentioned by Medopal.
Something like new File("").getAbsolutePath() might help me get the base Folder when executed on a local system. Likewise there is no such way to programmatically find out the working base when executed on a shared location.