I'm downloading a file from the FTP server using the below
StandardFileSystemManager -> resolveFile and copyFrom(fileobject, Selectors.SELECT_SELF)
The local folders were created automatically and file has been successfully downloaded in LINUX machine.
When i executed the same operation in windows machine i got the following exception, because it is a relative path, and no base URI was provided
org.apache.commons.vfs.FileSystemException: Could not find file with URI "/mnt/shared/\test\sample\files\monday\34.csv" because it is a relative path, and no base URI was provided.
Could you please let me know your thought and suggestions?
Thanks,
Kathir
Please specify your Windows path with forward slash as well. resolveFile() expects URIs not local files. You can use fo = manager.toFileObject(new File("test\\bla.txt")) instead of resolveFile if you insist on windows native (in this case relative) path.
Related
I developed a software in netbeans + Ubuntu and then converted the runnable .jar file of netbeans to .exe file using a converter software.
I used:
File f = new File("./dir/fileName");
which works fine in Ubuntu but it gives an error in Windows, because the directory pattern of both OSs are different.
Absolute paths should not be hardcoded. They should be read e.g. from a config file or user input.
Then you can use the NIO.2 File API to create your file paths: Paths.get(...) (java.io.File is a legacy API).
In your case it could be:
Path filePath = Paths.get("dir", "fileName");
I used: File f = new File("./dir/fileName") which works fine in Ubuntu but it gives error in Windows, bcz the directory pattern of both os are different.
It is presumably failing because that file doesn't exist at that path. Note that it is a relative path, so the problem could have been that the the path could not be resolved from the current directory ... because the current directory was not what the application was expecting.
In fact, it is perfectly fine to use forward slashes in pathnames in Java on Window. That's because at the OS level, Windows accepts both / and \ as path separators. (It doesn't work in the other direction though. UNIX, Linux and MacOS do not accept backslash as a pathname separator.)
However Puce's advice is mostly sound:
It is inadvisable to hard-code paths into your application. Put them into a config file.
Use the NIO2 Path and Paths APIs in preference to File. If you need to assemble paths from their component parts, these APIs offer clean ways to do it while hiding the details of path separators. The APIs are also more consistent than File, and give better diagnostics.
But: if you do need to get the pathname separator, File.separator is an acceptable way to get it. Calling FileSystem.getSeparator() may be better, but you will only see a difference if your application is using different FileSystem objects for different file systems with different separators.
You can use File.separator as you can see in api docs:
https://docs.oracle.com/javase/8/docs/api/java/io/File.html
I am trying to read the directory from another machine (files located under tomacat/webapps/ROOT/directory) and to display available files under that directory using URL class. can anybody suggest some ideas.
Thanks in Advance,
JAI
You can make a path with Paths.get("tomacat","webapps","ROOT","directory"), this should make the correct path on each OS. Then you can find all the files under that directory with: Directorystream ds = Files.newDirectorystream(Paths.get(...)). You can iterate through the result with for(Path file:ds){}
I have a license agreement file that needs to be open in default browser.
The file lies in installation folder itself.
I am doing it in java with awt as like this which is working fine:
Desktop d=Desktop.getDesktop();
d.browse(new URI("file://D:/OMS-Install/OMS/oms_license.txt"));
But since the entire folder can be placed anywhere on windows drive, at run time I need to consider the current directory. How can I achive this with Java & Default browser of AWT.
Doing it as there is a requirement. I would have otherwise followed many other options to accept terms and conditions.
Edit
Adding working code:
String path=new File("OMS/oms_license.txt").getAbsolutePath();
File license=new File(path);
URI urlLicense = license.toURI();
d.browse(urlLicense);
You can use Class.getResource() to retrieve the URL of something on the classpath.
Something along the lines of
URL license = getClass().getResource("/OMS/license.txt");
You can convert get file in current directory and calculate its absolute path
new File("./OMS/oms_license.txt").getAbsolutePath()
I'm trying to load a jasper report from my project's directory, but when I start the application, it gives me an error about file location, it says:
java.io.FileNotFoundException: C:\Program Files\Apache Software Foundation\apache-tomcat-7.0.50\bin\report.jasper
To get the file path, I use this:
new File("report.jasper").getAbsolutePath()
If I run that in a simple class, it gives me the right path, but when I run the application it gives me the tomcat's path, I tried some other functions like getCanonicalPath, getCanonicalFile and getAbsoluteFile; but it's always the same result.
Is there a solution respect this? My application will run on both platforms: Windows and Linux, it will be annoying place the report's file in the each respective tomcat's path everytime I update the application, I'm trying not to do that.
Thanks in advance.
If your file is located inside the web app root you should use servlet context to ask it the realpath
as javadoc explains
Another way is to user getClassLoader().getResourceAsStream() , but you can use it only if you don't have the need of knowing the filepath.
You can use Class#getResource(String name) or ClassLoader#getResource(String name) method both returns URL object and than you can use Url#getPath() to get the path.
I have created a Java application that loads some configurations from a file conf.properties which is placed in src/ folder.
When I run this application on Windows, it works perfectly. However when I try to run it on Linux, it throws this error:
java.io.FileNotFoundException: src/conf.properties (No such file or directory)
If you've packaged your application to a jar file, which in turn contains the properties file, you should use the method below. This is the standard way when distributing Java-programs.
URL pUrl = this.getClass().getResource("/path/in/jar/to/file.properties");
Properties p = new Properties();
p.load(pUrl.openStream());
The / in the path points to the root directory in the jar file.
Instead of
String PROP_FILENAME="src/conf.properties";
use
String PROP_FILENAME="src" + File.separator + "conf.properties";
Check the API for more detail: http://java.sun.com/j2se/1.5.0/docs/api/java/io/File.html
I would also check what your current working directory is if your path to that file is relative. You just need to make a File test = new File("."); and then print that files canonical path name.
If you are referencing any other locations like user.dir or something to that effect by using System.getProperty(), you'll want to at least verify that the directory you are using as the relative root is where you think it is.
Also, as Myles noted, check the slashes used as file path separators. Although you can always use the "/" and it works.
And if you are referencing the path absolutely, you'll have trouble going between one OS and another if you do something silly like hard-code the locations.
What you want to do is check out System.getProperties() and look for file.separator. The static File.pathSeprator will also get you there.
This will allow you to build a path that is native for whatever system you're running on.
(If indeed that is the problem. Sometimes I like to get the current directory just to make sure the directory I think I'm running in is the directory I'm really running in.)
Check your permissions. If you (or rather, the user that the Java process is running under) doesn't have appropriate permissions to read the file, for example, you would get this error message.
This is a typical Windows -> Linux migration problem. What does ls -l src/conf.properties show when run from a prompt?
Additionally, check capitalisation. Windows isn't case-sensitive, so if the file was actually called e.g. CONF.properties it would still be found, whereas the two would be considered different files on Linux.
You should check the working directory of your application. Perhaps it is not the one you assume and that's why 'src' directory is not present.
An easy check for this is to try the absolute path (only for debugging!).
I would check your slashes, windows often uses '\' vs linux's '/' for file paths.
EDIT: Since your path looks fine, maybe file permissions or executing path of the app is different?
check your slashes and colons
in my case i set my PS1 to following value
PS1='\n[\e[1;32m]$SYSNAME(\u)#[\e[1;33m]\w [\e[1;36m](\d \T) [!]\e[0m]\n\$ '
i am trying to read from the env .such as system.getenv
Java was throwing exception
java.lang.IllegalArgumentException: Malformed \uxxxx encoding
Try the double slash, after doing things in JBoss I often had to refactor my code to use the double slashes