In windows I can use this command:
java -cp server.jar;mysql.jar server.NithServer
But in linux it says:
-bash: server.jar: command not found
How can I add both server.jar and mysql.jar in Centos 5?
You need to use : as the path separator, instead of ;
In bash the ';' character separates shell commands when you want to put them in the same line.
This means that the string that appears after ';' and before the next space is considered a new command.
This is why, in the Unix java command, ':' is used as a separator for jars in the '-cp' option. This also happens in other Unix commands as well.
So you should write:
java -cp mysql.jar:server.jar server.NithServer
On another note, if you have these kind of difficulties again, try to call the 'man' command followed by the command you want to understand.
man java
There, you will find the answer to your question, right where the '-cp' option is described.
Enjoy.
Related
I have run the same command under windows using the windows Java Development Kit and it worked.Now i try to run it ubuntu and i get this error.Please help
Different OS have different dist package manager, you try to install Java environment in your Linux system by below Reference
It looks like you are specifying the -cp argument incorrectly.
On Linux, the path separator character is ":" not ";". The ";" character separates commands on the command line, assuming that you are using bash or similar as your shell.
It looks like your command has been interpreted as two commands:
$ javac -g -cp ../dist_windows_x86_64/something
$ . GPUGalaxySim.java
The javac command fails because there are no source filenames on that command line ... just like the error message says.
Then the second command:
The dot command (".") is a built-in shell command that "sources" a file and attempts to interpret it as shell commands.
If you attempt to "source" a file that is actually Java source code, you get nonsense error messages, basically because the shell has no clue what Java code means.
I want to use an environment variable as a JVM option when executing java -jar.
The command I want to execute it:
java -XX:onOutOfMemory='echo test' -jar foo.jar
When I run the above command as is, the jar will run.
(If you don't have the foo.jar, you will get an Error: Unable to access jarfile foo.jar error. But this still means that the option gets used correctly).
But when I create an environment variable containing the JVM option, and run the command using that variable.
OOM="-XX:onOutOfMemory='echo test'"
java $OOM -jar foo.jar
Than I get the following error:
Error: Could not find or load main class test'
It seems like the java command is ignoring the quotes around 'echo test'.
After looking for similar questions on SO and on other websites, I tried various variations of using quotes:
OOM="-XX:OnOutOfMemoryError=\"echo test\""
OOM='-XX:OnOutOfMemoryError="echo test"'
OOM=-XX:OnOutOfMemoryError="echo test"
But they all result in the same error.
An article from Oracle concerning JVM options, mentions using a semicolon:
-XX:OnOutOfMemoryError="<cmd args>; <cmd args>"
But the purpose of the semicolon is to separate multiple commands, not command and arguments. So this does not fix my problem.
Does anybody know how I can correctly move the -XX:onOutOfMemory='echo test' option into an environment variable?
When running java, you should quote $OOM
Example:
java "$OOM" -jar foo.jar
See Why does my shell script choke on whitespace or other special characters? on Unix stackexchange for why this is needed.
I am following this tutorial to make a program that interacts with the database. I am stuck at the last step where I run it. The given example is
C:\test>java -cp c:\test\postgresql-8.3-603.jdbc4.jar;c:\test JDBCExample
I have both the .class file and the .jar for the JDBC in my home directory. I tried
java -cp /home/JohnF/postgresql-9.2-1000.jdbc4.jar;/home/JohnF/QueryDB.class and I get "cannot exectue binary file"
I tried
java -cp /home/JohnF/postgresql-9.2-1000.jdbc4.jar;/home/JohnF/QueryDB and I get "no such file or directory"
I tried
java -cp /home/JohnF/postgresql-9.2-1000.jdbc4.jar;/home/JohnF QueryDB and I get "JohnF is a directory"
I used chmod to set the file permissions to 777. How do I get this to run?
You are using semicolon as classpath separator - this will not work on Linux.
Try replacing ";" with ":" in classpath and it should work.
Edit: explanation of what is happening here.
In Linux, ";" is command separator. Your line of
java -cp /home/JohnF/postgresql-9.2-1000.jdbc4.jar;/home/JohnF QueryDB
is really expanded into 2 executed one by one:
java -cp /home/JohnF/postgresql-9.2-1000.jdbc4.jar
/home/JohnF QueryDB
First one does nothing and successfully quits.
Second tries to invoke /home/JohnF as executable, and this is really not an executable, but a directory!
FIX: Use : instead of ;
WHY? The file-separator in *nix environment is ':' and not ';'
I have a Java code where I am importing Guava packages. I run it in windows command prompt using following commands:
javac -cp guava-11.0.2.jar Test.java
java -cp guava-11.0.2.jar;. Test
However, in Linux it is giving error. Can anybody help me to solve this issue.
The path separator on Linux/Unix is a colon, i.e. :.
So in your case the second command on Linux/Unix would be:
java -cp guava-11.0.2.jar:. Test
class path entries are separated by colons in Linux (not semicolons as in Windows)
Try that:
java -cp guava-11.0.2.jar:. Test
I'm trying to compile a Java project under Cygwin using a native Win32 Java.
The Java binaries are correctly found under /cygdrive/c/jdk/bin on my machine.
The following command works fine:
javac -d . ./gnu/kawa/util/PreProcess.java
The PreProcess.class file is generated in ./gnu/kawa/util/. Trying to invoke Java on this fails however:
CLASSPATH=.:$CLASSPATH java gnu.kawa.util.PreProcess \
%java6 +use:com.sun.net.httpserver +enable:XML \
`sed -e 's|\([^ ]*\)|./\1|' < ./patch-source-list`
Error: Could not find or load main class gnu.kawa.util.PreProcess
...
This command was invoked by make, that's where the $CLASSPATH variable is set dynamically. patch-source-list is just a list of class names. The : in the classpath looks suspicious, but I'm not sure how to test ; while not annoying sh.
My only other suspicion is that the native Java is trying gnu\kawa\util\PreProcess, but I think cygwin can transparently handle that.
Any ideas? Thanks for your time.
Another option would be to build your path using the ':' and then fix the results using cygpath. This is probably overkill in your specific situation, but in a general case where you may have references to multiple directories, some of which may be referenced as absolute rather than relative paths, or if you are working with cygwin symlinks, it is much more useful.
$ ln -s /cygdrive/c/development/libraries/ ../libs
$ cygpath -pw /cygdrive/c/development/:.:../libs
C:\development\;.;C:\development\libraries\
so then you'd build your CLASSPATH variable as before, and in the final stage run
CLASSPATH="`cygpath -pw "$CLASSPATH"`" java (whatever)
Remember, the JVM has no idea that you are using the cygwin bash shell.
Two things:
for the classpath locations, use the windows path names. Thus, no "/cygdrive/c/somepath", but "c:\somepath\" ("/" and "\" can be used interchangeably however)
use ';' instead of ':' in the classpath list
This should work:
export CLASSPATH="./gnu/kawa/util/PreProcess.class"
CLASSPATH=".;$CLASSPATH" java gnu.kawa.util.PreProcess
The : in the classpath looks suspicious, but I'm not sure how to test ; while not annoying sh.
You're exactly right: you need to use ; instead of :. As for how to use it — as Mat alludes to above, you need to "quote" the semicolon. Any of these will work:
CLASSPATH=.\;$CLASSPATH java Foo
CLASSPATH=.';'$CLASSPATH java Foo
CLASSPATH='.;'$CLASSPATH java Foo
CLASSPATH=".;$CLASSPATH" java Foo
You can use whichever one you like best. (The first uses a backslash, which quotes a single following character. The second and third use single-quotes, which quote a sequence of zero or more characters. The fourth uses double-quotes, which are like single-quotes except that they still allow the variable $CLASSPATH to be expanded. For that matter, you could also write something like
CLASSPATH=".;"$CLASSPATH java Foo
if you want. See the above link for lots more information about quoting in Bash.)