Classpath does not work under linux - java

Anyone have an idea why this command works fine in Windows but in Linux I get a ClassNotFoundException game.ui.Main
java -cp ".;lib/*" game.ui.Main -Xms64m -Xmx128m
my folder structure looks like this:
lib/ - Jars
game/ - Class files
This is the latest Java 6.

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.

Try changing the semi-colon to a colon.
The CLASSPATH separator is platform dependent, and is the same as the character returned by java.io.File.pathSeparatorChar.

Windows:
java -cp file.jar;dir/* my.app.ClassName
Linux:
java -cp file.jar:dir/* my.app.ClassName
Remind:
Windows path separator is ;
Linux path separator is :
In Windows if cp argument does not contains white space, the quotes is optional

Paths are important too when using classpaths in scripts meant to be run on both platforms: Windows (i.e. cygwin) and Linux. When I do this I include a function like this for the classpath. The 'cygpath' command with the '-w' option converts paths to Windows-style paths. So in this example "/home/user/lib/this.jar" would be converted to something like "C:\Cygwin\home\user\lib\this.jar"
#!/bin/bash
function add_java_classpath() {
local LOCAL1=$1
if [ "$OSTYPE" == cygwin ]; then
LOCAL1="$(cygpath -C ANSI -w $LOCAL1)"
fi
if [ -z "$JAVA_CLASSPATH" ]; then
JAVA_CLASSPATH="$LOCAL1"
elif [ "$OSTYPE" != cygwin ]; then
JAVA_CLASSPATH="${JAVA_CLASSPATH}:$LOCAL1"
else
JAVA_CLASSPATH="${JAVA_CLASSPATH};$LOCAL1"
fi
}
add_java_classpath /home/user/lib/this.jar
add_java_classpath /usr/local/lib/that/that.jar
java -cp "${JAVA_CLASSPATH}" package.Main $#

Related

how to convert this .bat (windows) to .sh (linux-Centos)

I have the following code within .bat and it works fine on Windows
* #java -classpath ..\QVDReader.jar;..\lib\opencsv-2.3.jar;..\lib\jdbm-3.0-SNAPSHOT.jar -Dfile.encoding=UTF-8 ExampleQVDReader .\QVD\Customer.qvd .\CSV\Customer.csv ";" pause *
Summary, it's a library, that convert QVD file to CSV (without problem). But when I want to try it in a Centos Server, in terminal it's wrong, and I don't know why, I used this
java -classpath ../QVDReader.jar;../lib/opencsv-2.3.jar;../lib/jdbm-3.0-SNAPSHOT.jar -Dfile.encoding=UTF-8 ExampleQVDReader ./QVD/Customer.qvd ./CSV/Customer.csv ";"
I need execute it the same in Linux, any ideas?
Greetings.
You don't mention what shell you're using, but I'll assume it's an sh variant, in which semicolons ; are special characters that separate commands. So:
java -classpath ../QVDReader.jar;../lib/opencsv-2.3.jar;../lib/jdbm-3.0-SNAPSHOT.jar -Dfile.encoding=UTF-8 ExampleQVDReader ./QVD/Customer.qvd ./CSV/Customer.csv ";"
Is broken up into multiple commands at each semicolon, to this:
java -classpath ../QVDReader.jar
../lib/opencsv-2.3.jar
../lib/jdbm-3.0-SNAPSHOT.jar -Dfile.encoding=UTF-8 ExampleQVDReader ./QVD/Customer.qvd ./CSV/Customer.csv ";"
To treat the semicolons literally, put the argument in single quotes:
java -classpath '../QVDReader.jar;../lib/opencsv-2.3.jar;../lib/jdbm-3.0-SNAPSHOT.jar' -Dfile.encoding=UTF-8 ExampleQVDReader ./QVD/Customer.qvd ./CSV/Customer.csv ';'
You can use double-quotes too, but be aware of the differences.

Linux equivalent of including the classpath during compilation

I'm following a guide that only includes compilation instructions on windows. How would one run this build.bat file on Linux?
The batch file looks like this:
#echo off
#echo Compiling...
javac -classpath ..\..\lib\OneWireAPI.jar;%classpath% -d . .\src\*.java
And when I run the javac command on Linux, it fails:
javac -classpath ../../lib/OneWireAPI.jar;%classpath% -d . ./src/ReadTemp.java
The output is:
javac: no source files
What is the correct way to do this?
On Linux, you have to use : (colon) in place of ; (semicolon) as the path separator in Java options.
Also, if you have a classpath variable, in most common Linux shells it is referenced by $classpath rather than by %classpath%
javac -classpath ../../lib/OneWireAPI.jar:$classpath -d . ./src/ReadTemp.java
You have two items that did not get translated correctly from Windows CMD to Unix:
Path separator ; should be :.
Environment variables should be changed from %classpath% to $CLASSPATH format. Note that pretty much everything is case-sensitive in Linux, including environment variable names, and the Java path is traditionally all-caps.
Try
javac -classpath ../../lib/OneWireAPI.jar:$CLASSPATH -d . ./src/ReadTemp.java

Passing multiple positional shell arguments to java application

I programmed a Java app. For complex reasons I cannot export it as an executable (due to CVS and environment promoting practices) to Linux. I also cannot add the main class path to the MANIFEST.MF via 'jar -cvmf' command because it is not installed in the Linux environment the app is running in (I have no control over what gets installed). The only other option I found was to create the following shell script:
#!/bin/bash
#check that parameters were passed
if [ $# -lt 2 ]; then
echo ""
echo "Not enough arguments provided. You must have at least 2 arguments with ISO SQL time stamps."
echo " After that you can have unlimited number of parameters for tools."
echo ""
exit 1
fi
echo "Recovering events that occurred between $#"
ROOT_DIR=_spool_generator
JAR_DIR=jar
mkdir $ROOT_DIR
mkdir $ROOT_DIR/$JAR_DIR
FULL_DIR=$ROOT_DIR/$JAR_DIR
cp /home/wma/jar/SpoolGenerator.jar ./$FULL_DIR/
echo $#
START=$1
END=$2
java -cp "./$FULL_DIR/SpoolGenerator.jar" com.btv.main.Driver $# # --> does not work
#java -cp "./$FULL_DIR/SpoolGenerator.jar" com.btv.main.Driver "2001-02-12 18:15:00.0" "2001-02-12 19:15:00.0" --> works
#java -cp "./$FULL_DIR/SpoolGenerator.jar" com.btv.main.Driver "$START" "$END" --> works
echo "Execution is complete..."
exit
The key issue here is that I have an unlimited number of parameters the application uses. This works great when deploying the java app directly as an executable in Windows, works fine if I specify the positional arguments the Shell script takes, but how do I pass these same arguments to the java app from within the Linux script. I have to pass the parameters to the script surrounded in quotes due to the timstamp's special characters, this seems to cause some aberrant parsing when the parameters are passed to the jar. I appreciate any help.

Ubuntu script to compile multiple java files?

I understand the command would be javac file_name.java but how would I put together a shell script which could compile several java files?
I was also thinking about copying the files, which I presume I just use cp and absolute file path referencing.
Create a .sh file and add the following contents. Make the file as executable and run it.
(Specify the complete path along with the file name)
#! /bin/sh
javac sample.java
Try this script: compile_java_files.sh
#!/bin/sh
typeset -r JAVA_FILES_DIR=$(cd full_path_to_java_files 2>/dev/null ; pwd) # JAVA FILES DIRECTORY
LOG_DIR="/tmp/java_compilation/logs" # Create this dir or use another one
for java_file in `ls $JAVA_FILES_DIR`;
do
javac $java_file
return_status=`echo $?`
if [ $return_status -ne 0 ]
then
echo "Failed to compile $java_file" >> $LOG_DIR/$java_file.ERR
exit 1
fi
done
Then run your script(don't forget to specify the path to the directory that contain java files):
chmod +x compile_java_files.sh
./compile_java_files.sh

powershell run java process problem

I'm trying to run a java process via Powershell in Windows XP. Here's the command:
java.exe -cp .;./common.jar -Dcontext=atest1 -Dresourcepath=. DW_Install
So, the classpath is . and .\common.jar (I think java takes the wrong slashes, right?) There are two environment variables, one "atest1" the other "." and the class to execute main on is DW_Install (in the default package).
This command works in cmd.exe, but doesn't is PS. What's going on? What is PS doing while parsing this command that CMD doesn't do (or vice versa)?
Aaron
The problem is that PS for some reason parses -Dresourcepath=. differently than cmd. What works is
java -cp '.;.\common.jar' -Dcontext=atest1 "-Dresourcepath=." DW_Install
It doesn't matter which way the slash goes, and it doesn't matter which quotes one uses (' or "). The classpath must be escaped, however, with some kind of quotes. A good test to see what's getting by the PS interpreter is to echo it. The following:
echo java -cp '.;.\common.jar' -Dcontext=atest1 -Dresourcepath=. DW_Install
yields the following output:
java
-cp
.;.\common.jar
-Dcontext=atest1
-Dresourcepath=
.
DW_Install
(Notice the resourcepath and the value of resourcepath are not on the same line.) Whereas the output to
echo java -cp '.;.\common.jar' -Dcontext=atest1 '-Dresourcepath=.' DW_Install
yields the following output:
java
-cp
.;.\common.jar
-Dcontext=etaste1
-Dresourcepath=.
DW_Install
Which is much more to our liking.
Although I wish this upon none of you, I hope that this post helps those of you that must deploy java projects on Windows machines (even though they will not run on any other platform ever).
Running external command-line programs from PowerShell is sometimes a bit problematic because there PowerShell exposes two different parsing modes that get trumped by the different syntaxes of said external programs.
In any case, running a command in Powershell requires using either the . prefix (dot-"sourcing") or the & operator.
You can workaround this by passing each parameter to the external program as separate variables, like so:
PS> $classpath = ".;./common.jar"
PS> $env = "-Dcontext=atest1 -Dresourcepath=."
PS> $class = "DW_Install"
PS> . java.exe -cp $classpath $env $class
Another example based on https://gaming.stackexchange.com/questions/24543/how-do-i-change-player-name-in-minecraft-multiplayer-in-offline-mode-in-linux
function mineCraftAs {
Param (
[parameter(mandatory=$true, HelpMessage="Minecraft character name." ,ValueFromPipeline=$true)]
[string] $name
)
if(!(test-path $env:appdata)) { $(throw "Appdata not found at $env:appdata")}
$private:minecraftPath=Join-Path $env:appdata .minecraft
if(!(test-path $minecraftPath)) { $(throw "Minecraft not found at $minecraftpath")}
$private:minebinPath=join-path $minecraftPath "bin"
if(!(test-path $minebinPath)) { $(throw "Minecraft bin not found at $minebinPath")}
$minebinPath | write-debug
gci $minebinpath | write-debug
#java -Xms512m -Xmx1024m -cp "%APPDATA%/.minecraft\bin\*" -Djava.library.path="%APPDATA%\.minecraft\bin\natives" net.minecraft.client.Minecraft '"'%1'"'
echo java -Xms512m -Xmx1024m -cp ('"'+$minebinPath+'\*"') ('-Djava.library.path="'+$minebinPath+'\natives"') net.minecraft.client.Minecraft ($name)
$minecraftJob=& 'C:\Program Files (x86)\Java\jre6\bin\java.exe' -Xms512m -Xmx1024m -cp ('"'+$minebinPath+'\*"') ('-Djava.library.path="'+$minebinPath+'\natives"') net.minecraft.client.Minecraft ($name)
}
minecraftas newbie
The following should work:
java.exe -cp '.;./common.jar' -Dcontext=atest1 -Dresourcepath=. DW_Install
I guess that PowerShell interprets the ; in the classpath as command delimiter, thereby trying to run java -cp . and ./common.jar -D....
start-process -nnw java "-cp .;./common.jar -Dcontext=atest1 -Dresourcepath=. DW_Install"

Categories

Resources