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
  • 51,252
  • 26
  • 196
  • 186
sproketboy
  • 8,454
  • 17
  • 61
  • 85

4 Answers4

116

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
  • 25,223
  • 12
  • 95
  • 113
peter.murray.rust
  • 36,369
  • 41
  • 146
  • 215
  • 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
  • 532
  • 1
  • 5
  • 10
Mikel
  • 23,754
  • 8
  • 63
  • 66
7

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
  • 1,635
  • 1
  • 14
  • 23
Wender
  • 935
  • 13
  • 23
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
  • 138
  • 8