File path encoding - java

Can you help me to get the correct file path? I give in the arguments a link to a file with the name:
GovHK 香港政府一站通:Homepage (Residents).webloc
And instead of it I receive:
GovHK ???????:Homepage (Residents).webloc
So I can not get file, file.exists(); returns false
public class Test {
public static void main(String[] args) {
if (args.length > 0) {
File file = new File(args[0]);
System.out.println("Exists: " + file.exists());
}
}
}
File was taken from MacOS to Windows.
You can see the original file here
How can I get the real file path to handle it?
Thank you!
UPD
If I get list of files in the directory, file path is showing correctly and files exist. So the problem is on way cmd -> jar

Thanks to #MikitaBerazouski question, we found out that it is an issue of Windows console encoding.
[UPDATE]
Used JNI to get real command line arguments

Related

File not found in user.dir

I have a simple line of code that should print out first line of a file:
System.out.println(new BufferedReader(new FileReader("file")).readLine());
The class file with this line is located in c:/users/my/project.
A file named "file" exists in c:/users/my/project.
If I open CMD, navigate to c:/users/my/project and run
java MyClass
The first line is printed out and everything is fine.
But if I navigate to C:/ in CMD, and run
java -Duser.dir=c:/users/my/project MyClass
I get a response that file "file" could not be found.
If I move "file" to c:/ and run the same command again then it is found.
As I understand it, changing the user.dir should be equivalent to me being in the folder that it points to but that doesent seem to be true. The class file is found in user.dir, but files are still found in the folder I ran the command in, not the folder that user.dir points to.
Why is this?
If you navigate to another directory and try to run your program with a different user.dir, you also have to set the classpath. The error you get is because it can't find the class, not because it can't find the file.
Try
java -Duser.dir=c:/users/my/project -classpath c:/users/my/project MyClass
Regarding the user.dir property, have a look at the documentation for the File class. It states
A pathname, whether abstract or in string form, may be either absolute or relative. An absolute pathname is complete in that no other information is required in order to locate the file that it denotes. A relative pathname, in contrast, must be interpreted in terms of information taken from some other pathname. By default the classes in the java.io package always resolve relative pathnames against the current user directory. This directory is named by the system property user.dir, and is typically the directory in which the Java virtual machine was invoked.
EDIT
I did a test on my machine and found that file reader looks in the directory that the application was launched from (which may not coincide with the user.dir property). See below:
public static void main(String...args) {
try {
System.out.println("abs path to test.txt : " + new java.io.File("test.txt").getAbsolutePath());
System.out.println("user home : " + System.getProperty("user.home"));
System.out.println("user.dir : " + System.getProperty("user.dir"));
System.out.println("running from : " + new java.io.File(".").getAbsolutePath());
System.out.println(new java.io.BufferedReader(new java.io.FileReader("test.txt")).readLine());
} catch (Exception e) {
System.out.println("Not found");
}
}
Launching this from a directory that doesn't have the file in it I get
C:\Users>java -Duser.home=C:\Users\william -Duser.dir=C:\Users\william -classpath C:\Users\william\Desktop Test
abs path to test.txt : C:\Users\william\test.txt
user home : C:\Users\william
user.dir : C:\Users\william
running from : C:\Users\william\.
Not found
Whereas launching it from a directory that has the file I'm looking for I get
C:\Users\william>java -Duser.home=C:\Users\william -Duser.dir=C:\Users\william -classpath C:\Users\william\Desktop Test
abs path to test.txt : C:\Users\william\test.txt
user home : C:\Users\william
user.dir : C:\Users\william
running from : C:\Users\william\.
hello world
ANOTHER EDIT
I'm using the openjdk 8, so in my case the logic is as follows, from its source:
FileReader constructor with String arg
public FileReader(String fileName) throws FileNotFoundException {
super(new FileInputStream(fileName));
}
FileInputStream constructors
public FileInputStream(String name) throws FileNotFoundException {
this(name != null ? new File(name) : null);
}
public FileInputStream(File file) throws FileNotFoundException {
String name = (file != null ? file.getPath() : null);
SecurityManager security = System.getSecurityManager();
if (security != null) {
security.checkRead(name);
}
if (name == null) {
throw new NullPointerException();
}
if (file.isInvalid()) {
throw new FileNotFoundException("Invalid file path");
}
fd = new FileDescriptor();
fd.attach(this);
path = name;
open(name);
}
FileInputStream open(String)
private void open(String name) throws FileNotFoundException {
open0(name);
}
The open0(String) method is a native call (see below)
private native void open0(String name) throws FileNotFoundException;
Digging deeper, into the native call in FileInputStream.c
JNIEXPORT void JNICALL
Java_java_io_FileInputStream_open0(JNIEnv *env, jobject this, jstring path) {
fileOpen(env, this, path, fis_fd, O_RDONLY);
}
Therefore the name is what becomes the path that the native fileOpen method will use. This translates to file.getPath(), which is different from file.getAbsolutePath().
file.getPath() with file as new File("test.txt") returns test.txt, which may not be found.
That's the best I can do, I hope it helps.
See this bug report and this question. In short, setting the user.dir via the command line is bad news.
You are getting FileNotFound exception because the program is not able to find the file you are trying to read (In your case fileName is also "file").
The problem is coming because you have defined only the file name not the path. You should either give absolute or relative path for it to make work from any directory. In absence of the path definition the program will try to read from a file in current directory from where your command is executing.
Let's dive into the details:
you have the text file in path : c:/users/my/project/file
You have your Java file in path : c:/users/my/project/MyClass
now when you run inside c:/users/my/project directory , you get the expected output because the text file "file" is defined in this directory only.
when you switch to c: and try to run the program, you get exception because it is trying to find "c:/file" which does not exist.
try to change the line to mention absolute path instead:
System.out.println(new BufferedReader(new FileReader("c:/users/my/project/file")).readLine());

About citing external files in java application

I am writing a java application, in which I am automatically importing external csv files in background to do the computation. But the problem is that I am using "absolute" file path in my java program, the generated jar file will not work in another computer. Is there anyway in java to use a kind of "working directory path" so that I can still run the jar file in another computer as long as I put the csv files I'd like to import in the same folder with the jar file?
Thanks!
You can read a file using its name like
try (BufferedReader br = new BufferedReader(new FileReader("text.txt"))) {
String line;
while ((line=br.readLine())!=null) {
System.out.println(line);
}
} catch (Exception e) {
e.printStackTrace();
}
Here text.txt should be in the same working directory where the jar was executed.
You can also read the directory name from the command line, using the command line arguments like
public static void main(String[] args) {
//check if there were any command line arguments
if (args.length > 0) {
// args[0] is the first command line argument unlike C where args[0] would give u the executable's name
} else {
System.err.println("Usage: java -jar <jar_name> [directory_names..]");
}
}
You can also have a configuration file such as a properties file to read the directory names.
new File(".") give you the relative path
you can write relative path like that :
File file = new File(".\\CSVs\\myfile.csv");
System.getProperty("user.dir") will return you the working directory.
System.getProperty("user.dir")+"\\myfile.txt"
More informations here :system properties, oracle docs

How to create a working .jar for a java project w/ excel input and System.out.print output?

I created this java project that basically gets data from an user determined excel file and uses Syste.out.println() to display the results. It works as I want it to in eclipse, however when I exported is as a .jar file, it doesn't work properly. It prompts for the excel file location to be entered, however, does not display the output, not even an error. I do not know how to do it from the terminal so I'm running it by double-clcking it. Also, I want the user to choose any excel file they want, so what should they write down as the location when prompted to do so? Right now, the excel file is in the same directory as the project. So just the name of the excel file is enough input, but what if it is not in the directory, how do i show it's location then?
Thank you
First of all, in your JAR file the file META-INF/MANIFEST.MF must contain your main class (i.e. the class with the main() method), with a line like this:
Main-Class: mypackage.MyMainClass
Make sure your settings in Eclipse are generating this line when generating the JAR file.
You can run it from the terminal with java -jar yourapp.jar however I presume that you have some extra libraries you need to include in the classpath with the -cp switch.
The working directory is normally the directory from where you are running the application. If you want to specify a different path it has to be either relative to that, or absolute.
An absolute path in windows would be something like: "C:\mydatafolder\myexcelsheet.xls"
An absolute path in Unix would be something like: "/home/myaccount/mydatafolder/myexcelsheet.xls"
You need to read System.in to get the file path, with a function like this:
private static String getFilePath () {
String filePath = null;
while (true) {
System.out.println("Please input the path of the file:");
try {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
filePath = br.readLine();
File file = new File(filePath);
if (!file.exists()) {
System.out.println("Sorry, invalid file path. Please try again.");
} else {
return filePath;
}
} catch (IOException e) {
e.printStackTrace();
System.out.println("Sorry, unexpected error happened, Please try again.");
}
}
}

Java File.listFiles() not able to retrieve Names from Absolute Path

import java.io.File;
public class FileDemo {
public static void main(String[] args) {
String sourceDirectory = "~/Documents";
System.out.println(sourceDirectory);
File dir = new File(sourceDirectory);
File[] dirFiles = dir.listFiles();
for (File file : dirFiles)
{
System.out.println( file.getName() );
}
}
}
I am using the code above to list files in the Documents directory in Ubuntu. The same code works if I replace the folder name to a local folder where the Java class file resides. HOwever, I always get NULL Pointer exception when using absolute paths, as the dirFiles is NULL.
Could someone explain if there is any mistake in my approach.
Thanks.
Tilda ~ is not absolute path. It is a feature of typical unix shell to replace it by home directory of current user. In java program you should use System.getProperty ("user.home") instead of tilda.
The problem seems to be with the sourceDirectory. Instead of ~/Documents , try with the complete path /home/foo/Documents

How to pass a text file as a argument?

Im trying to write a program to read a text file through args but when i run it, it always says the file can't be found even though i placed it inside the same folder as the main.java that im running.
Does anyone know the solution to my problem or a better way of reading a text file?
Do not use relative paths in java.io.File.
It will become relative to the current working directory which is dependent on the way how you run the application which in turn is not controllable from inside your application. It will only lead to portability trouble. If you run it from inside Eclipse, the path will be relative to /path/to/eclipse/workspace/projectname. If you run it from inside command console, it will be relative to currently opened folder (even though when you run the code by absolute path!). If you run it by doubleclicking the JAR, it will be relative to the root folder of the JAR. If you run it in a webserver, it will be relative to the /path/to/webserver/binaries. Etcetera.
Always use absolute paths in java.io.File, no excuses.
For best portability and less headache with absolute paths, just place the file in a path covered by the runtime classpath (or add its path to the runtime classpath). This way you can get the file by Class#getResource() or its content by Class#getResourceAsStream(). If it's in the same folder (package) as your current class, then it's already in the classpath. To access it, just do:
public MyClass() {
URL url = getClass().getResource("filename.txt");
File file = new File(url.getPath());
InputStream input = new FileInputStream(file);
// ...
}
or
public MyClass() {
InputStream input = getClass().getResourceAsStream("filename.txt");
// ...
}
Try giving an absolute path to the filename.
Also, post the code so that we can see what exactly you're trying.
When you are opening a file with a relative file name in Java (and in general) it opens it relative to the working directory.
you can find the current working directory of your process using
String workindDir = new File(".").getAbsoultePath()
Make sure you are running your program from the correct directory (or change the file name so that it will be relative to where you are running it from).
If you're using Eclipse (or a similar IDE), the problem arises from the fact that your program is run from a few directories above where the actual source is located. Try moving your file up a level or two in the project tree.
Check out this question for more detail.
The simplest solution is to create a new file, then see where the output file is. That is the correct place to put your input file into.
If you put the file and the class working with it under same package can you use this:
Class A {
void readFile (String fileName) {
Url tmp = A.class.getResource (fileName);
// Or Url tmp = this.getClass().getResource (fileName);
File tmpFile = File (tmp);
if (tmpFile.exists())
System.out.print("I found the file.")
}
}
It will help if you read about classloaders.
say I have a text file input.txt which is located on the desktop
and input.txt has the following content
i came
i saw
i left
and below is the java code for reading that text file
public class ReadInputFromTextFile {
public static void main(String[] args) throws Exception
{
File file = new File(
"/Users/viveksingh/desktop/input.txt");
BufferedReader br
= new BufferedReader(new FileReader(file));
String st;
while ((st = br.readLine()) != null)
System.out.println(st);
}
}
output on the console:
i came
i saw
i left

Categories

Resources