67

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.

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
sproketboy
  • 8,967
  • 18
  • 65
  • 95

4 Answers4

117

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.

chiborg
  • 26,978
  • 14
  • 97
  • 115
peter.murray.rust
  • 37,407
  • 44
  • 153
  • 217
  • Yes. Thanks. My eyes are going. It looked like a semi on the web page I was checking. :) – sproketboy Dec 24 '10 at 23:16
  • Anyone still around that can tell me how to figure this out in a script? like a python or shell script? so that I can run a script using a "platform independent" scripting language to start a program written in a "platform independent" compiled language? – Code Jockey May 09 '19 at 20:06
  • @CodeJockey: You can test for the existence of a directory that would exist only on Windows, or check one of the env variables that would only be set on Windows (PUBLIC, COMMONPROGRAMFILES, USERDOMAIN, ...). – Michaël Oct 09 '19 at 03:54
34

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.

pflaquerre
  • 531
  • 1
  • 5
  • 11
Mikel
  • 24,855
  • 8
  • 65
  • 66
8

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
Alex78191
  • 2,383
  • 2
  • 17
  • 24
Wender
  • 991
  • 15
  • 24
4

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 $@
pinkston00
  • 168
  • 9