简体   繁体   中英

Java classpath on Windows with custom packages

I have the following file structure for a Java project I've been working on:

classes/ X/ Y/ Z/

Inside X/ Y/ and Z/ are my .java file for different parts of the project. They are packaged together respectively. When I compile everything together, I send all of my .class files to the classes/ directory using this command:

javac -cp classes/jsoup-1.8.3.jar -d ./classes/ X/*.java Y/*.java Z/*.java

This compiles just fine (I had placed the JSoup JAR file in the classes directory prior to this). Inside Z/, there is a Main.java with an appropriately declared public static void main(String[] args) . This file, along with all others are compiled, and their .class files are all sent to the /classes directory.

Now, when I try to run Main.class using java Main from within the classes/X/ directory, I get the following error:

Error: Could not find or load main class Main

So I figured I would try messing with the classpath. I went two directories up and tried this:

java -cp jsoup-1.8.3.jar;./X/;./Y/;./Z/ Z/Main

But then all I got were instructions on how to use java, followed by

bash: ./X/: Is a directory
bash: ./Y/: Is a directory
bash: ./Z/: Is a directory 

ps I am using Git Bash in Windows, so that is why I got the bash error instead of a cmd or powershell error. I have also tried using colon as the delimiter instead of semicolon, but unless I was doing something else wrong, that also did not work.

Lets suppose you have:

myDir
X a.class b.class
-- Y d.class
-- Z MainClass.class

where X,Y and Z are directories.
Y and Z are subdirectories of X, X is a subdirecory of myDir.

Position yourself in > myDir and execute:

java -cp "your jars..." X.Y.MainClass

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM