I'm complete Linux newbie, but still want to provide a simple way for Linux users to start my Java program.
Therefore I want to create a shellscript.
I can't test my script so I'll have to ask here if this is working correctly:
#!/bin/bash
java -cp "bin";"extres/junit.jar" data.ProgramOne
exit 0
Your mistake is in path delimiter. It is ; on Windows and : on Linux.
Moreover you should not wrap each classpath fragment with "". On unix you can escape spaces and other forbidden characters using \. So, I'd re-write the java execution line as:
java -cp bin:extres/junit.jar data.ProgramOne
This will run when you are executing script from your app directory where you have subdirectory bin and extres.
try this:
java -cp "bin:extres/junit.jar" data.ProgramOne
Java under Unixes uses : as the separator in the classpath, so you'd need (the quotes are not necessary):
#!/bin/bash
java -cp bin:extres/junit.jar data.ProgramOne
Related
I've been trying to compile and run this example for javafx https://openjfx.io/openjfx-docs/#maven using Cygwin on Windows 10. It took awhile to get past the compile because my javafx is stored in C:\Program Files\javafx-sdk-13.0.1, the trouble being the space in the folder name. After trying lots of different things I finally found How to cd into a directory with space in the name?, which in a nutshell tells you to put quotes around your environment variable, "PATH_TO_FX".
Then I tried to run the example
$ java --module-path "PATH_TO_FX":mods -m hellofx/hellofx.HelloFX
Error occurred during initialization of boot layerjava.nio.file.InvalidPathException: Illegal char <:> at index 10: PATH_TO_FX:mods
So I thought the PATH_TO_FX was the problem but it turns out it is not.
$ java --module-path src:mods -m hellofx/hellofx.HelloFX
Error occurred during initialization of boot layerjava.nio.file.InvalidPathException: Illegal char <:> at index 3: src:mods
src is a valid directory and I still get the same problem. I think it is related to java being stored in a directory with spaces in it but I'm not sure.
#Ray_Write
isn't ; for Windows? Cygwin uses bash
This has nothing to do with the shell. The parsing of --module-path is handled entirely by the java interpreter, and according to the docs uses ; on Windows instead of :, presumably for congruence with Windows PATH separators.
Since this Java installation is a native Windows application and not one built for Cygwin, one should still use ;. So in effect, this has nothing to do with Cygwin.
For passing file paths to java you may also need to use cygpath to convert the path to its native Windows path.
You're right #Iguananaut, I do need a semi colon, to get my example to work I had to escape it.
java --module-path "$PATH_TO_FX"\;mods -m hellofx/hellofx.HelloFX
where PATH_TO_FX is in .bash_profile as
PATH_TO_FX="C:/Program Files/javafx-sdk-13.0.1/lib"
This is my exact batch file. I have tried to convert it doing some research online and get an error
"Failed to execute child process "/home/pi/Desktop/TeachVal/TeachValLinuxShell" (No such file or directory)
echo off
cls
echo Running TeachVAL II...
set path=%path%;/Library/Java/JavaVirtualMachines/jdk1.8.0_65.jdk/Contents/Home/bin
java -classpath comm.jar;Robot.jar;TeachVAL TeachVAL
cls
exit
This one is my attempt at translating.
#!/bin/bash
set +v
clear
echo "Running TeachVAL II..."
java -cp ".dir1;dir2;path/home/pi/Desktop/TeachVAL/comm.jar;
path/home/pi/Desktop/TeachVAL/Robot.jar;/home/pi/Desktop/TeachVAL/TeachVAL"
clear
exit
Welcome to Linux--life is good here, but there are a few things that work slightly differently, when compared to Windows.
One difference is that Windows uses semicolon (;) to separate entries in a list of paths, but Linux uses colons (:) for that purpose.
So, the Windows command:
java -classpath comm.jar;Robot.jar;TeachVAL TeachVAL
would correspond to this on Linux:
java -classpath comm.jar:Robot.jar:TeachVAL TeachVAL
In general, on Linux, semicolons are used to put multiple command lines into a single line. Once you've learned that, I think you can then understand why:
java -cp .dir1;/home/pi/Desktop/TeachVAL/TeachVAL
would be the same as:
java -cp .dir1
/home/pi/Desktop/TeachVAL/TeachVAL
That would run java (with no class to be executed) and then try to run "/home/pi/Desktop/TeachVAL/TeachVAL" which can't be found.
There are many more differences to learn; here's a page that will help you get started: http://tldp.org/LDP/abs/html/dosbatch.html
I am trying to run a Java application on a Mac. Currently, there is a batch file that works for Windows that looks like this:
java -cp lib/appframework-1.0.3.jar;lib/commons-net-3.1.jar;lib/mysql-connector-java-5.1.6-bin.jar;lib/swing-worker-1.1.jar;TimeCardApplicationOdesk.jar org.ep.gui.TimeCardApplication
This works fine on Windows, but running that command on a Mac outputs a bunch of gibberish (mostly "command not found" errors").
I have tried to set the class path beforehand and load the jars with the -jar switch, but I'm stumped and know nothing about Java. I'm sure there are some slight changes that need to be made to the syntax, but I'm lost.
Replace all semicolons (;) with colons (:) in the command.
Multiple path entries to the -cp flag are separated by colons on unix systems. Following examples from these docs (solaris and windows) illustrate this.
On a windows system:
C:> java -classpath C:\java\MyClasses;C:\java\OtherClasses ...
Note that the two paths are separated by a semicolon.
And on a unix like system:
% java -classpath /java/MyClasses:/java/OtherClasses ...
Note that the two paths are separated by a colon.
I made a java program. I used eclipse and it's a maven project. Now when i run the program from windows command prompt, then it's run fine. Here How i am running it from windows command prompt
D:\Personal Work\eclipse 32 Bit\workspace\....\target\classes>
java -cp ".;..\dependency-jars\*" com/softech/ls360/integration/BatchImport vintners
It is working fine. My dependency jar folder contains these jar files
Now when i run the same program from linux. Here how i am runnign it
root#Basit:/home/test/script/classes# java -cp .;../dependency-jars/*; com.s
oftech.ls360.integration.BatchImport vintners
Then i am getting the errors that
....
-javaagent:<jarpath>[=<options>]
load Java programming language agent, see java.lang.instrument
-splash:<imagepath>
show splash screen with specified image
../dependency-jars/commons-collections-3.2.1.jar: line 1: PK??: command not found
../dependency-jars/commons-collections-3.2.1.jar: line 2:
../dependency-jars/commons-collections-3.2.1.jar: line 2: ?8: command not found
../dependency-jars/commons-collections-3.2.1.jar: line 3: syntax error near unex
pected token `)'
../dependency-jars/commons-collections-3.2.1.jar: line 3: ? ¶META-INF/MANIFE
ST.MF?VKo
_¦?z? ?%+v?N??!ö!P#
(
_?o.5?$
com.softech.ls360.integration.BatchImport: command not found
Why i am getting these errors. How can i run it on linux? Please help
Thanks
You need to use : instead of ; in classpath on linux envrionment. Assuming you have jars placed properly, then simply changing this:
java -cp .;../dependency-jars/*; com.s
oftech.ls360.integration.BatchImport vintners
to
java -cp .:../dependency-jars/*: com.s
oftech.ls360.integration.BatchImport vintners
should work
Learn more about setting classpath here: http://docs.oracle.com/javase/tutorial/essential/environment/paths.html
The semicolon is making Bash invoke the java command with no classpath, then tries to execute each jar directly, seeking a shebang where none exists. This leads to JAR headers being printed as part of the error.
Use : to separate jars instead of semicolons on Linux.
You need to make two changes:
First, the classpath separator is ':' rather than ';' on Linux
Second, you'll need to escape the wildcard character with a backslash ('\'), or the shell will expand it and mess things up. You want Java to see the '*' character and expand it itself. The Windows shell doesn't expand wildcards on command lines, so this isn't a problem there.
So, all together, you'll want to use something like
java -cp .:../dependency-jars/\*: com.softech.ls360.integration.BatchImport vintners
You should use : instead of ; as a sperator of the class path files.
I'm trying to write a BASH script to get my Java program to run(common issue, right?). I just can't quite get it to work. After many edits, here's how I am trying to set the classpath and then execute the program:
java -classpath 'cygpath -u "/cygdrive/c/Projects/common/lib/rome-1.0.jar:/cygdrive
/c/Projects/common/lib/jdom-1.0.jar:/cygdrive/c/Projects/common/lib/jsoup-1.6.1.jar:
/cygdrive/c/Projects/common/lib/mysql-connector-java-5.1.18-bin.jar:/cygdrive/c/Projects
/Freereader/bin/"' com.free.syndication.SQLfeeder
Sorry the the jumble, I'm just trying to do everything at once. It tells me that the main class of my program cannot be found!((
Any ideas?
Java classpath uses semicolon as the token separator.
Use backticks instead of single quotes
Try:
java -classpath `cygpath -u "/cygdrive/c/Projects/common/lib/rome-1.0.jar;/cygdrive
/c/Projects/common/lib/jdom-1.0.jar;/cygdrive/c/Projects/common/lib/jsoup-1.6.1.jar;
/cygdrive/c/Projects/common/lib/mysql-connector-java-5.1.18-bin.jar;/cygdrive/c/Projects
/Freereader/bin/"` com.free.syndication.SQLfeeder
In bash, the syntax $(command) is clearer than the backticks `command`
cygpath has a -p option to convert PATH-like values (as opposed to single path names) between Windows and Unix, i.e.
cygpath -pu 'C:\Users\me\bin;C:\Users\me\project\bin' will give /cygdrive/c/Users/me/bin:/cygdrive/c/Users/me/project/bin
cygpath -pw will do the same in the opposite direction
Note that cygpath -u "/cygdrive/c" (as in your question) will not change anything, since the directory name is already in the desired (Unix) syntax. You could omit it just as well.
So, the command becomes:
CP="C:/Projects/common/lib/rome-1.0.jar;C:/Projects/common/lib/jdom-1.0.jar;C:/Projects/common/lib/jsoup-1.6.1.jar;
C:/Projects/common/lib/mysql-connector-java-5.1.18-bin.jar;C:/Projects
/Freereader/bin"
# for a Windows Java binary:
java -classpath "$(cygpath -pw "$CP")" com.free.syndication.SQLfeeder
# for a Unix Java binary:
java -classpath "$(cygpath -pu "$CP")" com.free.syndication.SQLfeeder
Alternatively, you can start with a Unix-style class path, but the commands stay the same. In either case, you can of course omit the call to cygpath if the class path is already in the desired syntax.
Don't you need backticks?
java -classpath `cygpath -u "/cygdrive/c/Projects/common/lib/rome-1.0.jar:/cygdrive
/c/Projects/common/lib/jdom-1.0.jar:/cygdrive/c/Projects/common/lib/jsoup-1.6.1.jar:
/cygdrive/c/Projects/common/lib/mysql-connector-java-5.1.18-bin.jar:/cygdrive/c/Projects
/Freereader/bin/"` com.free.syndication.SQLfeeder
You must use backticks ( '`' symbol ) or $(cmd) bash sytax to substitute cmd output
java do not understand unix- (cygwin-) style paths, only windows-style.
And at last first link in google answers you question
The main cause of the issue is NOT the backtic but instead the issue of colon versus semi-colon. Since in cygwin, the java running there is for DOS/Windows environment it is expecting ';' as the path separator.
While backtic does help, the main root cause of the issue must be emphasize the difference between ':' and ';' when Java is in Unix or in Windows environment.