Classpath does not work under linux

JavaLinuxClasspath

Java Problem Overview


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.

Java Solutions


Solution 1 - Java

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.

Solution 2 - Java

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.

Solution 3 - Java

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

Solution 4 - Java

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 $@

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionsproketboyView Question on Stackoverflow
Solution 1 - Javapeter.murray.rustView Answer on Stackoverflow
Solution 2 - JavaMikelView Answer on Stackoverflow
Solution 3 - JavaWenderView Answer on Stackoverflow
Solution 4 - Javapinkston00View Answer on Stackoverflow