Add Jar File to Buildpath in Windows Command Line - java

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.

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.

Related

Compiling and running in java. Giving me errors

compiled my project in netbeans IDE and when I run the jar nothing happens just gives me a error occured while trying to run it. I think I found the problem just not sure how to fix it. Help please!
This is what happens when I check the jar in CMD
C:\Users\Mac\Desktop>java -jar dist\pong.jar
Exception in thread "main" java.lang.UnsatisfiedLinkError: no lwjgl in java.libr
ary.path
at java.lang.ClassLoader.loadLibrary(Unknown Source)
at java.lang.Runtime.loadLibrary0(Unknown Source)
at java.lang.System.loadLibrary(Unknown Source)
at org.lwjgl.Sys$1.run(Sys.java:73)
at java.security.AccessController.doPrivileged(Native Method)
at org.lwjgl.Sys.doLoadLibrary(Sys.java:66)
at org.lwjgl.Sys.loadLibrary(Sys.java:95)
at org.lwjgl.Sys.<clinit>(Sys.java:112)
at org.lwjgl.opengl.Display.<clinit>(Display.java:135)
at mw.Main.initDisplay(Main.java:36)
at mw.Main.main(Main.java:25)
C:\Users\Mac\Desktop>
When I check the manifest file it's pretty much empty:
Manifest-Version: 1.0
X-COMMENT: Main-Class will be added automatically by build
when I check the jar in CMD C:\Users\Mac\Desktop>java -jar dist\pong.jar
You need to use the -cp switch while running the jar using the java command.
Documentation
You can easily google to find more examples on how does -cp works.
You need to create a library and add a reference to it in your project
See Setting Up LWJGL with Netbeans
At a guess I'd say you are missing step 5 from Setting Up a Project to Use LWJGL in NetBeans
Finally, you need to tell NetBeans where the Native Libraries (i.e, DLL, JNILIB, DYLIB, SO files) are for your system so that the natives are linked when running.
Select the Run category and then type the following into the VM Options, replacing what is in bold to suit your system:
-Djava.library.path=<lwjgl-X.X path>/native/<linux|macosx|solaris|windows>
Update: Didn't realise you were doing it from the command prompt. You say you are running
java -jar dist\pong.jar
You need to add the -D option so something like this:
java -Djava.library.path=<lwjgl-X.X path>/native/windows -jar dist\pong.jar

No main found when executing jar

http://pastebin.com/1btVw8Cb
There are two classes in the above code.
So above is my code which is working fine when I hit run in Eclipse, runs fine in Netbeans as well.
I am trying to create a standalone application, a jar file.
The error I get when I double my jar is:
Could not find the main class: NewJFrame. Program will exit.
I get the following from the command promt:
E:\Java Programs\Eclipse Workspace\test3\src\test3>java testT.jar
Exception in thread "main" java.lang.NoClassDefFoundError: testT/jar
Caused by: java.lang.ClassNotFoundException: testT.jar
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
Could not find the main class: testT.jar. Program will exit.
I followed the command from here: "Could not find the main class: XX. Program will exit."
So this is what I am typing in the command promt to create my jar:
E:\Java Programs\Eclipse Workspace\test3\src\test3>jar cfm MyJar.jar manifest.tx
t *.class SINGLE.TXT
http://imgur.com/a/wlCpc#SYR3L
Some shots above I took to show the process.
So I think the problem could be error when I do javac? But it builds fine and runs fine in netbeans and eclipse >< Please help.
You need to use the -jar flag.
java -jar MyJar.jar
http://docs.oracle.com/javase/tutorial/deployment/jar/run.html says
JAR Files as Applications
You can run JAR-packaged applications with the Java interpreter. The basic command is:
java -jar jar-file
The -jar flag tells the interpreter that the application is packaged in the JAR file format. You can only specify one JAR file, which must contain all the application-specific code.
and http://docs.oracle.com/javase/tutorial/deployment/jar/modman.html explains how to put a manifest in the jar.
The m option indicates that you want to merge information from an existing file into the manifest file of the JAR file you're creating.
it is now working. I have not changed anything, maybe the memory got curroupted and that is why it was giving me problems. I think restart the computer is what fixed it. But yea, I don't know for sure what was wrong, though it is working fine now. So thank you for your help guys
Add
Main-class: Splash
part to Manifest file. set Main class name instead Splash
That will work...

Unable to run compiled .classes from the command line

I am having some issues running my compiled java code from the command line. I have written it and compiled it using the IntelliJ IDE (where everything runs fine if done within the IDE), but wish to now run it from the command line.
Compiling from the command like (using javac) also works fine, but running (with java) does not.
I am almost certain this is a classpath issue but cannot seem to fix it. From my searching prior to posting this I found a post telling me to run the "set PATH=\%PATH\%;"C:\Program Files\Java\jdk1.6.0_21\bin" command and then try running java. I have also tried various arguements I have found for -cp and -classpath. The error is :
Exception in thread "main" java.lang.NoClassDefFoundError: Share/class
Caused by: java.lang.ClassNotFoundException: Share.class
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
Could not find the main class: Share.class. Program will exit.
You're doing:
java -cp ... Share.class
Do
java -cp ... Share
Or if it's in a package
java -cp ... path.to.Share
You should not be supplying the class file as an argument, you should be supplying the fully qualified class name.
If your class is in the current directory and uses the default (empty) package, it will just be
java -cp . Share
or
java Share
The classpath is not used to point to the java executable, it's used to point to the various directories/jar files which contain your class files (at the root of the package structure).
See also
java - the Java application launcher (manual for invoking java)

ClassNotFoundException thrown when compiling basic Hello World Program

I am just trying to compile and run a very simple test program, but it simply will not work, and I have no idea what the problem is.
I have a java project that's been heaped on me, and I know little to nothing about java. Especially compiling from the windows command line.
I have two Jars that I need to compile a simple "hello world" program with.
Here's my "build.bat"
C:\jdk1.6.0_21\bin\javac -cp "C:\Users\FREYERA\Desktop\Test";"C:\Users\FREYERA\Desktop\Test\test1.jar";"C:\Users\FREYERA\Desktop\Test\test2.jar"; "C:\Users\FREYERA\Desktop\Test\sample.java"
Then, I:
C:\jdk1.6.0_21\bin\java sample
This spits back the error:
Exception in thread "main" java.lang.NoClassDefFoundError: sample
Caused by:
java.lang.ClassNotFoundException:
sample
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native
Method
at java.net.URLClassLoader.findClass(URLClassLoader.java:190
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
No matter how I set up my classpath, I cannot for the life of me get this HELLO WORLD program to run.
Can someone please help me out? I'm pulling my hair out.
You can also specify the classpath for the interpreter to locate your class:
java -classpath "C:\Users\FREYERA\Desktop\Test";"C:\Users\FREYERA\Desktop\Test\test1.jar";"C:\Users\FREYERA\Desktop\Test\test2.jar"; Sample
This would run your class from any working directory.
Run your program from the directory with the class in it:
C:\Users\FREYERA\Desktop\Test\>java sample
CLASSPATH (normally) includes the current directory.
If you have "sample.class" in the current directory, and you also need classes in test1.jar and test2.jar, this should work:
java -cp "test1.jar;test2.jar;." sample
Afer running this command
C:\jdk1.6.0_21\bin\javac -cp "C:\Users\FREYERA\Desktop\Test";"C:\Users\FREYERA\Desktop\Test\test1.jar";"C:\Users\FREYERA\Desktop\Test\test2.jar"; "C:\Users\FREYERA\Desktop\Test\sample.java"
It would have created a .class file at this location "C:\Users\FREYERA\Desktop\Test\". i.e. sample.class
You need to either go to this folder location and run your java command to execute the program. Make sure that your "JAVA_HOME" environment variable is set.
Or you can copy the sample.class file to "C:\jdk1.6.0_21\bin\" folder and run the command.

Jar not found when executing class

I'm working through a ANTLR (a language processing library) book and there are many examples that should be easy to compile using the command line.
Some information to get te problem:
antlr-3.2.jar contains the library classes. I added the antlr-3.2.jar to the CLASSPATH environment variable (Windows 7) and when compiling the classes with javac everything works fine.
This is what i execute to compile my program:
javac Test.java ExprLexer.java ExprParser.java
Test.java contains my main()-method whereas ExprLexer and ExprParser are generated by ANTLR. All three classes use classes contained in the antlr-3.2.jar. But so far so good. As I just said, compiling works fine.
It's when I try to execute the Test.class that I get trouble.
This is what I type:
java -cp ./ Test
When executing this, the interpreter tells me that he can't find the ANTLR-classes contained in the antlr-3.2.jar, altough I added an entry in the CLASSPATH variable.
Exception in thread "main" java.lang.NoClassDefFoundError: org/antlr/runtime/Cha
rStream
Caused by: java.lang.ClassNotFoundException: org.antlr.runtime.CharStream
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
Could not find the main class: Test. Program will exit.
I'm using Windows 7 and Java 1.6_20. Can someone tell what is going on? Why will the interpreter not look in the jar-Archive I specified in the CLASSPATH?
I found some kind of workaroud. I copied the antlr-3.2.jar into the directory where the Test.class is located and then executed:
java -cp ./;antlr-3.2.jar Test
This worked out. But I don't want to type the jar-Archive everytime I execute my test programs. Is there a possibility to tell the interpreter that he should automatically look into the archive?
I'm using Windows 7 and Java 1.6_20.
Can someone tell what is going on? Why
will the interpreter not look in the
jar-Archive I specified in the
CLASSPATH?
-cp on the commandline overrides the CLASSPATH variable. There is no convenient way to do what you're trying to do. I'd suggest creating an ant script, shell script, or shell alias if you don't want to type out the full classpath each time.
Alternatively, you could put your Test application into its own jar file with a manifest that tells it to include antlr-3.2.jar in the classpath.

Categories

Resources