0

I am compiling a program with multiple jar files (inside the lib folder) and classes (inside the src/com folder) with:

javac -classpath lib/\* src/com/*.java

I typed this to run the program:

java -cp lib/\* src/com/okc

But it doesn't work. Instead, I get this:

Error: Could not find or load main class src.com.okc

okc.java is the class containing the main method. How can I run a java program with multiple jar files and classes?

Luis Cruz
  • 1,182
  • 2
  • 15
  • 42

3 Answers3

2

A Java class file is not just the file itself. The directory structure which represents a class's package is part of the class file. Your classpath needs to point to the directory which is the parent of the topmost package directory.

Assuming your class is declared with package com;, the topmost package directory is com. So you need the parent of com in your classpath:

java -classpath src:lib/\* com.okc

If your class does not contain any package statement, and you just happened to put it in a com directory, then it belongs to the null package, whose parent directory is com itself:

java -classpath src/com:lib/\* okc

An additional note: It is Java convention to have class names, and their respective file names, start with an uppercase letter. One reason is that it makes class names easy to distinguish from package components.

VGR
  • 33,718
  • 4
  • 37
  • 50
0

Try:

java -cp ../lib/\* com.okc

from the src directory (not sure...)

shlomi33
  • 1,352
  • 6
  • 8
-1

Assuming that your current directory has your lib/ :

java -cp lib src.com.okc