Cant input file into mac OS folder, fileNotFoundException - java

Try to insert file to existing local folder. But seems like there's any security or permission issue
String path = con.getConfigValue("CustomPathImgUpload"); //return /Users/name/Documents/
File file = new File(path + date + "_" + fileName);
Got error below
java.io.FileNotFoundException: /Users/name/Documents/20221010_abc.xlsx (No
such file or directory) at
java.base/java.io.FileInputStream.open0(Native Method) at
java.base/java.io.FileInputStream.open(FileInputStream.java:211)
Check users folder , system read write
check name folder , name read write
check documetns folder, name read write
Eclipse apps and chrome apps already get full disk access
Get this problem after update from OS bigsur to menterey
How to solve this issue?
EDIT
Try to change the path to /Users/ and /Applications/ still folder location not found.
Make sure it's not source code mistakes by run it in windows eclipse. File saved correctly
ada usr/bin/java , jawaw, javap, etc to full disk access still not work
EDIT 2
try to use System.getProperty("user.home") + File.seperator + date + filename
return "/Users/name/20221010_abc.xlsx" But still get no such directory :'(
Is it because my Users folder permission looks like this?
OR is there any way to check what path is exist ?

Related

Hot to get user folder with graalvm native using "user.dir" on Linux

So i am having a problem to get user "user.dir" file location. when I compile while not in native mode I get the normal path that I can also point to in the file manager but when I build a native image all I get is "root" as user folder, is there another way to correct this because I can even call root as a folder path to act on the file system.
public static void printAllGuaranteedProperties() {
printAProperty ("user.dir", "User's current working directory");
}
public static void printAProperty (String propName, String desc) {
System.out.println ("Value for '" + desc + "' is '" + System.getProperty(propName) + "'.");
}
the sample code above , just try to run this you will the results difference , I was using jdk 17 community edition for graalvm . make sure you build and image and test this as well .
I think there is a small confusion here. user.dir is for working directory and .home is for home folder.
"user.dir" User working directory
"user.home" User home directory
check https://docs.oracle.com/javase/tutorial/essential/environment/sysprop.html has more system properties
I used your code and called this rest service and print the output you can see the result below

How to pass variable file path to SQL*Loader on Linux

I'm creating .csv file and placing it in one location.
String location = "/APPL_TOP2/IMDD/apps/apps_st/appl/xxfin/12.0.0/bin/xe.csv";
Using exact path working fine on Windows but I need it to work on Linux client too.
String location = "$XXFIN_TOP\\12.0.0\\bin\\xe.csv";
If I'm using relevant path on Linux it is not working, showing Error
SQL*Loader-500: Unable to open file(/APPL_TOP2/IMDD/apps/apps_st/appl/xxfin/12.0.0/bin/xe.csv)
SQL*Loader-553: file not found
SQL*Loader-509: System error: No such file or directory
" Client is asking any option to pass relevant path client machine is Linux"
Paths in Linux have slashes which go the other way. So it should be this:
$XXFIN_TOP/12.0.0/bin/xe.csv

NetBeans Java Project from command line: Working directory is System32

If I run my Java program in NetBeans and follow the information given in the output window to run from a command line:
To run this application from the command line without Ant, try:
java -jar "C:\Users\erdik\OneDrive\Documents\Computing Science Degree\Course Folder\Year 1\Programming 1\Assignment 2 - Year 2 Edit\assignment2\dist\assignment2.jar"
The program starts to run, but when it comes to run the following code to open a .txt file (my "database"):
System.out.println("Loading database of stored transactions...");
try
{
file = new File("TransactionDetails.txt");
inFile = new Scanner(file);
}
// if the log couldn't be found in the default program location
catch (FileNotFoundException ex)
{
System.out.println(CustomMessages.FileNotFound() +
System.getProperty("user.dir")); // display default directory
System.out.println(CustomMessages.systemExit());
System.exit(1); // the program needs the log to function as intended
}
It cannot find the .txt file and prints the default directory as the Windows System32 folder. How can I specify the location to be the Project folder as expected?
You could use an absolute path to the file instead of a relative path. e.g
file = new File("C:\Users\erdik\OneDrive\Documents\Computing Science Degree\Course Folder\Year 1\Programming 1\Assignment 2 - Year 2 Edit\assignment2\dist\TransactionDetails.txt");
inFile = new Scanner(file);
You cannot rely on the current working directory to be set to anything.
Either provide the file as a class path resource instead or ask the jvm where the class is located in the file system and locate the file relative to that.
For a read only file I would consider providing it as a resource.

Java console app running as a cron job having issue with relative path

In my java program, I am trying to save a .csv file into my data folder located in the same folder as the main jar file.
Previously, when I used to run my programs on Windows machines, my relative path was: data\\foo.csv. When I tried the same on Linux, it created and saved the file with name: data\\foo.csv in the root directory.
Then I tried to set the path to data/foo.csv and I am getting this error:
java.io.FileNotFoundException: data/04-12-2015.csv (No such file or directory)
at java.io.FileOutputStream.open(Native Method)
at java.io.FileOutputStream.<init>(Unknown Source)
at java.io.FileOutputStream.<init>(Unknown Source)
at java.io.FileWriter.<init>(Unknown Source)
at main.Main.saveResultsToFile(Main.java:121)
at main.Main.main(Main.java:92)
I have set permissions of the directory to 777 (granted all permissions to everyone).
Code responsible for creating and saving the file:
String fileName = "data/foo.csv"
BufferedWriter bw = new BufferedWriter(new FileWriter(fileName));
Edit:
The permission is not recursive, if that changes anything. Only the data folder has 777 permission.
I encountered the same problem today, the post is old but people may end up here so:
The problem is crontab runs from the root directory so relative paths start from the root (/) and get a null point exception. On cronjob you can precede your command with cd $jar.directory
Assume you have your jar file in /home/project/data and want yo run every night:
> crontab -e
> 0 0 * * * cd /home/project/data && /usr/bin/java -jar program.jar >> log.txt 2>&1
In Java there is a field in File called separatorChar which is a exactly what you need to build platform independent file names. There is also a field called separatorwhich is a String version of the same thing. Makinging a path is then like String fileName = "data" + File.separator + "foo.csv" ;
As the error is file not found and not a complaint about permissions, permissions are not the problem. Presumably then you either are trying to open a file that is not there, or you are have not put the file where cron expects it.
Try the following :
File f = new File( "data" + File.separator + "foo.csv" ) ;
System.err.println( "Path being used is : " + f.getCanonicalPath() ) ;
This should report the resolved pathname that is being used from your relative path name. It should at least tell you where the file is being looked for by your cron job.

IOException when creating a temporary file?

I'm creating a task plugin for Atlassian Bamboo. At some moment of task executing, I would like to create a temporary file:
File temp = File.createTempFile(fileName.toString(), null, dir);
temp.deleteOnExit();
, where:
fileName.toString() = e.g. "C:\Atlassian\bamboo-home\xml-data\build-dir\CMPT-CMPTP-JOB1\test.java"
dir = new File("temp");
When testing this locally, everything works fine - the file is created properly. However, after I deploy plugin on server and try to execute above code, I've got an IOException:
java.io.IOException: The filename, directory name, or volume label syntax is incorrect
at java.io.WinNTFileSystem.createFileExclusively(Native Method)
at java.io.File.createTempFile(File.java:1879)
What could be the reason?
Additional info: I'm pretty sure that dir.exists() .
A file name of
"C:\Atlassian\bamboo-home\xml-data\build-dir\CMPT-CMPTP-JOB1\test.java"
is valid on Windows but is invalid on Unix operating systems. You won't be able to create a (temp) file like that, either as specified as the absolute name/path or the file nor just relative to another folder.
If your OS is Windows, you still can't use a full path (starting with drive specification like "C:") to be created as a child of another folder.
You could have spaces in the beginning or the ending of your path, print your file.getAbsolutePath() in order to see the current path where java is reading.
The dir variable must be set with the full (or relative) path to the directory temp. The first arg of File.createTempFile should be the prefix of the temp file (at least three letter long. for exeample "test"). This will create a "test.tmp" in the given directory (specified by the variable dir).
Check the javadoc
You can check existence of the directory dir with dir.exists()

Categories