I am working on a simple program in java, which I use to read and write a few things in one SQLITE database, located within the directory I have the jar file. To access the database and a settings file, I use the
String wd = System.getProperty("user.dir");
String dbName = "jdbc:sqlite:"+wd+"\\"+sqliteDataNae;
c = DriverManager.getConnection(dbName);
Long story short,
I have made a jar file wrapping all libraries in and tested it in Windows where everything works fine. I get my data stored in my database. Not exception at all!
Then I transfer all my files in a rar file to an ubuntu (14) hyperv virtual machine.
I use the terminal to go to my directory where I have unzipped everything, and run:
java -jar myJar.jar
The script starts running, I get my println and the setting are all loaded (hence no issue reading the settings txt file - located in the same directory as the database = no directory issue),
The problem is that I get an sqlite exception [SQLITE_ERROR] SQL error or missing database (no such table: User).
However, I open my database (using SQLite Database Browser) and the table exists.
Is there any logical explanation for this? and is there any solution?
My guess is since Linux uses forward slashes for path separators you'll need to replace '\\' with '/'. However to make life easier just use File.separator instead as it automatically gives you the default separator for the current OS.
You should use File.pathSeparator it's used to separate individual file paths (Every OS uses different kind of Path variable).
For windows, it’s ‘\’ and for unix it’s ‘/’.
e.g: File file = new File("abc"+File.separator+"def.txt");
//on Unix system prints: abc/def.txt
Related
Google Web Toolkit (GWT) generates huge number of temporary files in the temp (C:\Users\User01\AppData\Local\Temp) directory.
Example of a file path:
C:\Users\User01\AppData\Local\Temp\gwt-codeserver-1101830889369654349.tmp\com.company01.web.builder.BuildingsWeb\compile-2\gen\com\company01\web\theme\custom_pluto123\client\base\progressbar\Css3ProgressBarAppearance_Css3ProgressBarTemplate_render_SafeHtml__SafeHtml_text__Css3ProgressBarStyles_style__SafeStyles_wrapStyles__SafeStyles_progressBarStyles__SafeStyles_progressTextStyles__SafeStyles_widthStyles___SafeHtmlTemplatesImpl.java
The above file path contains 437 characters.
When I tried to remove this type of files from Windows Explorer, it got crashed. Also I've tried to remove or rename it from command prompt it says The filename or extension is too long.
Finally I deleted by running custom java program.
Now, my question is why Windows couldn't able to remove it? If its not supported by OS, how java removes it?
Note:
I tried all of the above commands/actions with proper UAC (Run as administrator) in Windows 7 Ultimate and the File System was NTFS
Windows had an limitation of 260 characters (=MAX_PATH) but now also allows to create paths with up to 32,767 characters through the Unicode version of its API.
Windows Explorer sadly cannot handle long paths.
Java seems to use the Unicode API and therefore can create and remove long paths.
Resources:
https://support.microsoft.com/en-us/kb/320081
https://msdn.microsoft.com/en-us/library/aa365247%28VS.85%29.aspx
I have already read some posts but I cant solve my problem yet.
I am working on a remote desktop and windows server 2008. In the shared disk E: I put some batch file. This batch files call a new batch file from server which runs java script. And now I am taking the this message.
java.exe is not recognized as an internal or external command, operable program or batch file.
I try to set the environment like :
First setup the JRE7 to disk E
Second create new user variable which name is JAVA_HOME and which path is my JRE path E:\Tool\BatFiles
But I am still taking this error. Where should I do wrong ?
Setting JAVA_HOME is a good step, and with it you should be able to run Java as follows
%JAVA_HOME%/java myProgram arg0
If you don't want to include %JAVA_HOME in your command, you will have to include it in your PATH. Windows checks it's PATH for bin scripts every time a command is called. A typical Java installation does this for you.
You can edit your PATH to include ;%JAVA_HOME% at the end. Restart your command prompt for changes to take effect.
Edit 1
Be careful when editting your PATH however! Windows depends on it to function in many aspects. You can expect explorer to stop working. Make sure before altering your PATH variable, that you back it up somewhere. Just in case.
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.
I have annoying problem with non-latin characters in names of uploaded files. I'm using upload method pretty similar as in the Play's doc (extended by DB operations) and while running dist vrsion on the unix machines, everything is OK. In Windows - which is a production server - non-latin characters are damaged, for an example:
return ok(uploadFile.getFilename());
gives me: zażółć_gęśla_jaźń.png instead of zażółć_gęśla_jaźń.png
How can I avoid this and/or force reading and writing filenames always with the same encoding?
Edit
I just discovered, that when starting app with play start (instead of dist + start.bat) filename is written properly... that just confuses me even more o.O
The working solution is forcing it while starting the jar in the start.bat script by adding param:
-Dfile.encoding=utf-8