问题
I'm having an issue getting JSOUP going on my computer. I've downloaded JSOUP1.8.3 from the web and have tried many things to get it going.
I'm running JRE7 on Windows 7. I feel like I learn more using the command line so I've stuck with that. I type my programs in NotePad++ and use the command line to run them.
Here's what I've tried: 1)setting CLASSPATH to point to my JSOUP.jar file. This works with compiling but when I try to run my file I get the error "Could not find or load main Class" 2)unpacking the JAR file, adding the JSOUP folder to the ORG folder then recompiling. This didn't work and caused an error (Cant remember the error but my computer didn't like it.) 3)using the -cp switch. Again this works for compiling but not for running.
To compile:
javac -cp c:\js\jsoup.jar WS1.java
This works with no issues. When I try to run it with
java -cp c:\js\jsoup.jar WS1
I get the error "Could not find or load main class."
I'm not sure what other info you need to help me with this issue. I've been working on this for hours and just can't figure it out. Let me know if you need more info - Thanks - JF
回答1:
Thanks to Dariusz this problem is resolved. In order to use a JAR file through the command line you have two options:
1)use the -cp switch. For example, to compile:
javac -cp c:\yourFolder\yourJarFile.jar yourSourceCode.java
Then to run your java program:
java -cp c:\yourFolder\yourJarFile.jar;. yourClassFile
yourJarFile in this case was jsoup.jar
yourClassFile was WS1 and
yourSourceCode was WS1.java
2) Your second option is to create/add the following to your classpath system variable:
c:\yourFolder\yourJarFile.jar;.;%classpath%
Option 2 is the one I went with. This allows me to not have to use the -cp flag everytime I compile/run my java code.
The main thing that was holding me up here was the period or dot. I knew that I needed to add the current directory but didn't know how. So, make sure that you add the location of your added JAR file and the period/dot for the current directory and you'll be good to go.
THANK YOU ALL FOR YOUR HELP!!! JF
来源:https://stackoverflow.com/questions/34641095/jsoup-issues-could-not-find-or-load-main-class