Opening Files with Java while Working in Cygwin - java

I am running Cygwin on a Windows 7 machine, and using script files to execute Java programs in batch. My problem is this: I try to pass in a Cygwin / Linux path to a file, via the command line, and Java converts all of the forward slashes to backslashes.
For instance:
java program $scratchname/path_to_folder/ filename_$i.txt
Within Java, I take the directory and add the file name to open the file, which usually works with no issues as long as I'm using a Windows command line. However, in Cygwin Java converts this to:
home\scratch\path_to_folder
which Cygwin doesn't like.
I don't think this is a simple matter of replacing the backslashes with forward slashes, because Java seems to default to the Windows path conventions when I try to open the file. I'm guessing this is because Cygwin is pointed to the Windows installation of the JVM.
How can I force Java to use Cygwin / Linux path name conventions on a Windows system?

Java is a Windows program, and as such, only understands Windows paths; launching it from a Cygwin shell can't change that. You can use cygpath to convert paths back and forth.
Reference link: https://cygwin.com/cygwin-ug-net/using-effectively.html
Example case:
java -jar path/to/your-1.0.jar "$(cygpath -aw /home/YOUR_USER/path/to/file.txt)"
Options:
a provides the absolute path
w uses the Windows format

Related

Why does CMD not recognize the javadoc command?

I installed JDK on windows 10, 64bit, followed the documentation instructions.. CMD does not recognize javadoc command. What I tried:
-copied the path of the "bin" folder from Java and in cmd I wrote the command:
set path = "full_path_to_java_sdk_bin_folder"
I saw this on youtube and it worked for the guy, my cmd still did not recognize javadoc command -I set the PATH in system variables from control panel->system->advanced->environment variables and made sure that there are is no other bin folder...
Didn't find any other tips online...
The usual approach for Java development is to set a JAVA_HOME environment variable, and use that to update the PATH (for one thing, it makes it much easier to support multiple versions of Java). Also of note is that Windows puts the quotes oddly on the command line (oddly compared to every other platform that is) and if your path contains spaces you need to quote it correctly. Like,
set "JAVA_HOME=<full_path_to_jdk>"
set "PATH=%JAVA_HOME%\bin;%PATH%"

OS-independent way of specifying the class path

I have a bash script that bootstraps a Java process. I want it to be able to run on *nix and Cygwin. The problem is the separator in the -classpath parameter is different under the two platforms (: under *nix and ; under Windows).
I can't find an environment variable that specifies this separator (same as $PATH separator), so is there a better way to solve this than detecting the OS and hard-coding?
No. If you write your bootstrap code in Java rather than Bash, you can use the path.separator system property.
Otherwise, it's quite normal to see separate launcher scripts for both Unix and Windows.

Launch java app in specific version with bash script

I wish to open certain java applications (For example, MapTools, part of RPtools) with Oracle Java 6 for compatibility purposes using shell scripts.
In the simplest possible terms, what do I need to do to make a working shell script for an application to launch with a specific java version on Ubuntu?
Note that for maximum usefulness, specific application names should not be used. Instead use tags such as "App" to determine where the name of the application or it's path should go.
Specify absolute path of java executable of the JRE version,
[JRE_HOME]/bin/java -jar jar_name.jar
e.g.
/usr/lib/jvm/oracle-7-jre/bin/java -jar exec.jar
Use the full path to the desired java.exe in your command line.
See Run a JAR file using a specific JRE.

Java errors (hsqldb)

I am trying to start a jar file on Linux:
javac db.java #building
java -cp hsqldb.jar:. db
The code works fine with the JavaEditor on Wine:
http://pastebin.com/KVDqYydb
Looking at your code, this seems to be the problem:
new Datenbankzugriff(".\\Datenbank\\meineDB");
Linux uses forward slashes as path separator. Just convert the backslashes to forward slashes, then it should work (also on windows):
docs:
The database file path format can be specified using forward slashes in Windows hosts as well as Linux hosts.

Bat file for jar

I'm using a bat file to run my jar. The code in my bat file is this :
#echo off
java -cp analyser.jar be.model.Start
pause
This works fine for windows.
But it doesn't do anything at linux. I also need to be sure it will run on Mac
Bat files are specific to Windows. You would need to execute the command in Linux and Mac in a manner that is specific to those platforms. The actual java call should work the same, I believe. The one change to the java line would be if you had multiple items in the classpath. In that case, you would need to use a colon as a separator instead of a semicolon (which is what Windows uses). (Thanks to khachik for that tip)
For Linux, you would use Shell programming using a BASH script. Here is a link that will describe what you need to do:
http://www.tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html
For Mac, you would probably use an AppleScript. Here is an article on how to get started with AppleScripts:
http://www.macosxautomation.com/applescript/firsttutorial/index.html
For Linux, why not use a .sh (shell) file?
As Biggs~ alreay said, the actual Java call should remain the same.
Update:
You will also have to make it executable by changing it's user permissions. To do this, use: chmod +x thescript.sh

Categories

Resources