开发者

Classpath does not work under linux

Anyone have an idea why this command works fine in Window开发者_开发知识库s 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 $@
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜