类路径在 linux 下不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4528438/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 02:17:38  来源:igfitidea点击:

Classpath does not work under linux

javalinuxclasspath

提问by sproketboy

Anyone have an idea why this command works fine in Windows but in Linux I get a ClassNotFoundException game.ui.Main

任何人都知道为什么这个命令在 Windows 中工作正常,但在 Linux 中我得到一个 ClassNotFoundException game.ui.Main

java -cp ".;lib/*" game.ui.Main -Xms64m -Xmx128m

my folder structure looks like this: lib/ - Jars game/ - Class files

我的文件夹结构如下所示:lib/ - Jars game/ - Class files

This is the latest Java 6.

这是最新的 Java 6。

采纳答案by peter.murray.rust

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.

由于与文件系统密切相关,命令行类路径语法取决于操作系统。例如:

在所有类 Unix 操作系统(如 Linux 和 Mac OS X)上,目录结构采用 Unix 语法,用冒号(“:”)分隔单独的文件路径。

在 Windows 上,目录结构具有 Windows 语法,每个文件路径必须用分号 (";") 分隔。

这不适用于在清单文件中定义类路径的情况,其中每个文件路径都必须用空格 (" ") 分隔,而与操作系统无关。

回答by Mikel

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.

CLASSPATH 分隔符取决于平台,并且与java.io.File.pathSeparatorChar返回的字符相同。

回答by pinkston00

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"

在打算在两个平台上运行的脚本中使用类路径时,路径也很重要:Windows(即 cygwin)和 Linux。当我这样做时,我为类路径包含了一个这样的函数。带有 '-w' 选项的 'cygpath' 命令将路径转换为 ​​Windows 样式的路径。所以在这个例子中“/home/user/lib/this.jar”将被转换为类似“C:\Cygwin\home\user\lib\this.jar”的内容

#!/bin/bash

function add_java_classpath() {
  local LOCAL1=
  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 $@

回答by Wender

Windows:

视窗:

java -cp file.jar;dir/* my.app.ClassName

java -cp file.jar;dir/* my.app.ClassName

Linux:

Linux:

java -cp file.jar:dir/* my.app.ClassName

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
  • Windows 路径分隔符是 ;
  • Linux路径分隔符是 :
  • 在 Windows 中,如果 cp 参数不包含空格,则引号是可选的