While learning about the behaviour of the NIO2 API I have considered this:
Path unix = Paths.get("/");
Path windows = Paths.get("c:\\");
System.out.println(unix.getNameCount());
System.out.println(windows.getNameCount());
... gives the output
0
1
Why is that? I would expect the same result which should actually be 0 because there is no name but only a root. When I add a folder
Path unix = Paths.get("/etc");
Path windows = Paths.get("c:\\etc");
System.out.println(unix.getNameCount());
System.out.println(windows.getNameCount());
... then I get
1
1
Isn't that confusing for the Windows part?
Edit: I'm on a linux machine myself.
Alright, now I found the right explanation:
A Path instance reflects the underlying platform. In the Solaris OS, a Path uses the Solaris syntax (/home/joe/foo) and in Microsoft Windows, a Path uses the Windows syntax (C:\home\joe\foo). A Path is not system independent.
From here: http://docs.oracle.com/javase/tutorial/essential/io/pathClass.html
That means in my case on a linux machine the path "c:\\\\" would be the name of a relative folder within my working directory.
Related
On Windows I run the following command and it work;
java -cp "./libs/*;" SampleJavaApp
When I try to run the same command on Linux (CentOS 6) I get
Error: Could not find or load main class SampleJavaApp
SampleJavaApp has no package
Any insight as to why would be appreciated.
Thanks
UPDATE
The Java Version was the problem, as well as the :
The format of the classpath (-cp argument) uses the operating system path separator, to match the behavior of PATH. So you want : instead of ; for separating paths.
Also, you seem to be using an empty path element when I think you want to explicitly reference the current directory ..
Also, I think the handling of the * wildcard varies by Java implementation, so you need to make sure the versions match.
I am currently writing some code in which I work with files a lot. I implemented all file paths processing (concatenation, normalization, etc) using the Java 7 nio classes Paths and Path. On Windows everything works as expected however on Linux the Paths class behavior seems to be broken.
For example the following code:
System.out.println(File.separator);
System.out.println(FileSystems.getDefault());
Path path = Paths.get("../dir1/", "\\dir2\\file1").toAbsolutePath().normalize();
System.out.println(path);
if(path.toFile().exists()) {
System.out.println(path + " exists");
}
on Windows prints the following output:
\
sun.nio.fs.WindowsFileSystem
D:\projects\dir1\dir2\file1
true
but the same code on Linux Ubuntu 14.04 on both Java 1.7.0_79 (64 bit) and Java 1.8.0_60 (64 bit) leaves the path un-normalized:
/
sun.nio.fs.LinuxFileSystem
/home/semi/dir1/\dir2\file1
Also even if the file is at path /home/semi/dir1/dir2/file1 exists it is reported as non-existent by path.toFile().exists().
I looked a bit over LinuxFileSystem.java and WindowsFileSystem.java and it seems that on windows the path is checked for both / and \ characters (in WindowsPathParser.isSlash(char c) method). Shouldn't the Linux implementation do the same?
Is this a bug in sun.nio.fs.LinuxFileSystem implementation or am I doing something wrong?
Also do you know any alternative to make sure Linux paths are parsed and normalized correctly (without doing all the parsing manually).
In Windows, / is not a valid character in a filename so any code can assume it's a wrongly typed path separator.
In Linux, just about any byte is acceptable in a filename. In your Paths.get(), you're effectively joining a path called dir1 (1 level deep) and a path called \dir2\file1 (also 1 level deep).
Java on Windows is compiled against Microsoft C++ standard libraries, which permit either backslash or forward slash as a valid path separator. So backslash is a valid path separator only when running on Windows, whereas forward slash is valid everywhere.
The only time backslash is required, even on Windows, is when creating command lines that will be passed to CMD.EXE, to PowerShell, and possibly other terminals/shells. There are also some open source libraries that require backslash in file paths (when running on Windows), but java itself permits either forward or back slash in file paths.
IMHO, using File.separator when parsing input often leads to avoidable errors, whereas it has a valid role when generating output to be consumed by software or human eyeballs.
Okay I need both java and C compilers to work on my pc.
So I have set the path variable of C and now I need to set java's path, so for that can I set the path variable of java in the same PATH (where I set path for C) or do I need to create a separate PATH for setting it up? Some one please help.
If I set both in a same path, then will anything go wrong? Sorry I am new to this, please bear with me. Thanks
It would matter if there were any name collisions. To my knowledge there's no such, but since there are many C compilers and much more stuff for them, and the most important java stuff starts with 'java', just add the Java path AFTER the gcc/mingw path
SET PATH=%PATH%;%MINGW_PATH%\bin;%JAVA_PATH%\bin
I am introducing you to what is PATH variable ?
PATH is an environment variable on Unix-like operating systems, DOS, OS/2, and Microsoft Windows, specifying a set of directories where executable programs are located. In general, each executing process or user session has its own PATH setting.
On POSIX and Unix-like operating systems, the $PATH variable is specified as a list of one or more directory names separated by colon (:) characters
On DOS, OS/2, and Windows operating systems, the %PATH% variable is specified as a list of one or more directory names separated by semicolon (;) characters.[3]
Note : The above is an excerpt from WIKI
For Windows, you can set the path
SET PATH=%PATH%;%MINGW_HOME%\bin;%JAVA_HOME%\bin
For Unix-like operating systems, you can do
SET PATH=%PATH%;\home\%MINGW_HOME%\bin;\home\%JAVA_HOME%\bin
I need to create a directory on unix machine. I think the below code will work fine on unix machine but fails while testing it on local windows machine. Where does this directory get created on my local machine ?
String xmlDir = "/home/data/logs"
File xmlDirectory = new File(xmlDir);
xmlDirectory.mkdir();
I tried below directory path and it worked fine on windows machine. But i had to use the mkdirs() instead of mkdir() method which needs to be used for unix directory creation?
String xmlDir = "C:\\home\\data\\logs"
File xmlDirectory = new File(xmlDir);
xmlDirectory.mkdirs();
How can I make it work locally as well as n unix machine ? Is there a better way for File and Directory creation ?
--Thanks--
You should use the System user.home property which will return the user's home directory in a system independent manner, for example...
File home = new File(System.getProperty("user.home"));
mkdir will only create the last element in the path, where as mkdirs will create all the elements that do not exist. Using mkdirs is probably a slightly better idea as it ensures (where permissions allow) that all elements in the path will be created if they do not exist
You have already hit on the answer: Just use mkdirs(). It is not platform dependent. However, if you include platform dependent nomenclature, then you'll run into trouble when moving the code from one environment to another. Just be sure to use platform independent code, or at the very least, check for the OS before doing so via System.getProperty("os.name");
I know this question has been asked/answered several times, but I still couldn't find a solution to this ClassNotFoundException error, because it works on my computer but not on my RasPi (on which I installed OpenJDK7).
My application uses JDBC to access a MySQL database, and that's the main problem. As has been pointed out on lots of websites ([1], [2], [3]), this is, unfortunately, a common problem.
So, I'm using the mysql-connector-java-5.0.8.jar as a driver. My folder structure is something like /src/de/web/project/ I'm calling the main method via java de.web.project.WakeOnLan (which is the main class that starts all other classes etc.) I got the common ClassNotFound exception and therefor added the -cp parameter so I called the project via java -cp .;mysql-connector-java-5.0.8.jar de.web.project.WakeOnLan to add the driver to classpath.
This worked nicely on my Windows computer from command line, but now I want to push this code to my Raspberry Pi and execute it there. As I said, I installed OpenJDK7 there (using apt-get, if this should be important), used SFTP to upload the folder structure and the code to /home/pi/java/ where there is also the ejre1.7.0_10 folder (so the code is now actually in /home/pi/java/de/web/project/).
I now went back to /home/pi/java and entered java -cp .;mysql-connector-java-5.0.8.jar de.web.project.WakeOnLan as I did on my Windows computer, but it refuses to work (I've put the connector in any folder on the way, just in case). When I call this function, I get a long list of hints Java wants to give me which parameters are allowed for java, finally stating: -bash: mysql-connector-java-5.0.8.jar: command not found. I also tried to turn ".;mysql..." around to "mysql...;." which didn't work as well. If I don't include the -cp parameter, my program says "Thread started" and in the next line: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver etc., so the program does indeed start and writes "Thread started" to System.out.
For some reason, it looks like Java on the Pi ignored the second value of the classpath parameter for which I don't see a good reason.
You might just have problem in classpath setting, there is difference for windows and linux
The classpath syntax is OS-dependent. From Wikipedia :
Being closely associated with the file system, the command-line
Classpath syntax depends on the operating system. For example:
on all Unix-like operating systems (such as Linux and Mac OS X), the
directory structure has a Unix syntax, with separate file paths
separated by a colon (":").
on Windows, the directory structure has a Windows syntax, and each
file path must be separated by a semicolon (";").
This does not apply when the Classpath is defined in manifest files,
where each file path must be separated by a space (" "), regardless of
the operating system.