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 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.
About your second question : if you use
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
Found your problem, I think.
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
. Sincesrc
does not appear in your package names, it should also not appear in your compile/execution paths.So you should
and run
and
from there.
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.
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.
In version 6 you can use wildcards for the jars e.g.
but it might be cleaner putting your jars into a sub directory e.g.
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:
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.