Add Jar File to Buildpath in Windows Command Line
Im quite annoyed at having to ask this but I cant get it to work. Currently I have a project with:
5 Classes in the src/ folder
2 JARS named profiles.jar and classifier.jar in the root folder
I want to create a "makefile?" or "batch file?" to compile and run these classes FROM THE WINDOWS COMMAND LINE, but first add the jars 开发者_Go百科to the buildpath? Im not sure how I go about this
When I try to do it, it says that the class is not found, most likely due to me not adding the jars to the buildpath correctly. What are the commands I need to use to run this in command prompt?
Thanks Philip
EDIT
Thanks for the help, Im having alot of trouble getting it to work tho Currently I have a project with 5 classes in the src folder, and 2 jars in the jar folder
Here are the commands Im running:
set CLASSPATH=C:\wamp\www\news\UserProfiling\jars\classifier.jar ;C:\wamp\www\news\UserProfiling\jars\profiles.jar
Then from the root folder, Im running:
javac src/*.java
Then:
java -cp ./src:./jars/* src/Interaction
Interaction is the main class, Im getting all sorts of noclassfound errors, am I doing something wrong? Many thanks Philip
THE ERROR
java -cp ./src:./jars/* Interaction Exception in thread "main" java.lang.NoClassDefFoundError: Interaction Caused by: java.lang.ClassNotFoundException: Interaction at java.net.URLClassLoader$1.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at java.lang.ClassLoader.loadClassInternal(Unknown Source) Could not find the main class: Interaction. Program will exit.
In version older than or equal to Java version 5 you must specify each jar individually, and the root of your source, on your classpath e.g.
java -cp a.jar:b.jar:c.jar:./src MainClass
In version 6 you can use wildcards for the jars e.g.
java -cp ./src:* MainClass
but it might be cleaner putting your jars into a sub directory e.g.
java -cp ./src:./jars/* MainClass
So basically, your makefile or start script needs to construct a command like one of the above.
More info - Sun docs (v6)
Update - in response to your second edit, you need to specify the full main class name, so if the class is in a package called 'com.mypackage.MainClass' then you need to do:
java -cp ./src:./jars/* com.mypackage.MainClass
I'd also suggest getting the command working as a standalone command first, before getting the whole script running. By removing moving parts it will be faster to debug and easier to see what's going on.
I would suggest you take a look at ant or maven. Ant is a solution to do pretty much straightforward what you want to do, maven is not as straightforward but has its advantages when it comes to managing dependencies.
About your second question : if you use
java -cp ./src:./jars/* src/Interaction
it will try to launch the class src/Interaction, which does not exists. src is already in your classpath, so you just have to do
java -cp ./src:./jars/* Interaction
Found your problem, I think.
javac src/*.java
java -cp ./src:./jars/* src/Interaction
This problem plagues many beginners: You need to run javac and java from the directory where your source tree starts. In your case, that directory is src
. Since src
does not appear in your package names, it should also not appear in your compile/execution paths.
So you should
cd UserProfiling/src
and run
javac *.java
and
java -cp .:../jars Interaction
from there.
精彩评论