I have a toy program that is called Test.class. It accesses a class in a jar called myjar.jar. That jar is in my CLASSPATH variable set as part of my Windows environment. When I type echo %CLASSPATH%, I see C:\myclasses\myjar.jar. When I execute my program
java Test
it runs fine.
But if I package the code as a jar and try running
java -jar Test.jar
It ca
It can't find my classpath. I know this has a simple solution.
Can you please help me.
When -jar (or -cp or -classpath) argument is been used, then the %CLASSPATH% will be ignored. Instead, the Class-Path entry in JAR's /META-INF/MANIFEST.MF file will be used. You'd like to put the JAR-relative path to the other JAR in there. E.g.
Class-Path: myjar.jar
The above example expects the myjar.jar to be in same folder as the JAR file you'd like to execute.
An alternative is to package the 3rd party JAR inside your JAR file. In for example Eclipse you can do this.
First off I would not bother with this stuff myself anymore(I used too) and let my IDE(Netbeans/Eclipse) figure this stuff for you out. BTW I already hope you are using an IDE because it makes programming in Java that much more fun.
Next I would advice to learn a build tool like maven.
Related
I am trying to run a simple Java application in Unix. My Java application read a config file from a directory at run-time. I placed the files in /tmp/paddy/. I created a simple bash script to run a application.
I tried like below and it gives me "no main manifest attribute, in app.jar" error
#!/bin/bash
java -cp ".:./config/*.*" -jar "app.jar" com.test.MainClass
And tried with below command This time my application is running but couldnt find the aconfig file so it throw me NullPointerException - (since it couldnt load the config file)
#!/bin/bash
java -cp app.jar com.test.MainClass
What is the correct way to override classpath in Java -cp command ? I was searching over the internet, but couldnt get any good answers. I dont have any issues running in windows. Only in linux and I am pretty new to the linux environment.
You have four separate issues here.
-jar and -cp don't work together
If you use the -jar switch, the classpath is taken from the Class-Path manifest entry in the jar's manifest, and that is all that will happen - the -cp switch (and the CLASSPATH environment variable) are completely ignored. The solution is to fix your jarfile, which ought to have that classpath entry.
That's not how bash works.
Separate from that issue, your -cp parameter is broken.
*.* in.. linux...? That's late 90s DOS, mate!
It's java doing the parsing of that *, which is unique, because in linux it's normally bash doing it, but that doesn't work here, because bash will be adding spaces, and java needs colons/semicolons, which is why java does it itself. The point is, java is rather limited and only understands a single *. Which bash will mess up. So, there is really only one way to do this.
Single quotes.
One star.
For example:
java -cp '.:./config/*' com.test.MainClass
You don't seem to understand how classpaths work
Each individual entry in a classpath must be either:
A directory which contains classfiles.
A jar file
Note how it specifically cannot be 'a directory that contains jar files', and also cannot be 'a class file'; that is not a thing. The * is the usual treatment: It takes every file in the directory you padded with /* and considers them all to be part of the classpath.
So, if you write: java -cp ., that will not include app.jar. If you write java -cp './config/*', that will not include any class or config files hanging off of ./config (only jar files located there).
That's not how config files work
Including config files on the classpath is not how its done. You can, of course. This doesn't do anything whatsoever, unless you are using SomeClass.class.getResource or some other variant of getResource (those are no good, you should be using SomeClass.class.getResource or SomeClass.class.getResourceAsStream, but I digress), in which case, don't do that. Those aren't intended for config files, those are for static files (files that never change, such as, say, a 'save to cloud' icon for your swing user interface application). If you are doing that, you'd need to include ./config (and not './config/*') in your classpath, but it would be a better idea to fix your code.
config files should be in the user's home directory - System.getProperty("user.home"). You should consider the directory that contains the jar file(s) as the place where the executables live, and those are not necessarily editable by the user, and surely the point of a config file is that you can edit them. Hence why using the classpath for these is not how it is done.
I'm having big problems running my java api war file from the command line after ive packaged it with maven.
I'm trying to run it using the following command from the target folder where my war file is located.
java -cp silverkissen.war se/consys/silverkissen/heroku/Main
And alot of other variations but i just get
Error: Can't find or load main class se/consys/silverkissen/heroku/Main
My war file lies in path ..\Silverkissen-API\target\silverkissen.war
My heroku main class lies in path ..\Silverkissen-API\target\classes\se\consys\silverkissen\heroku\Main.class
Thankful for any help.
The main issue is most likely because the class files isn't incorporated into the silverkissen.war file. Meaning there is no Main function in the war file itself. Or that the entry function is some where else.
Or that it's packaged in some mysterious way that is beyond my understanding that's specific to maven, heroku etc.
But assuming you're standing in the project root structure, one level before the target folder usually where you'd normally have src, target, pom.xml and system.properties. I'd try running the following:
java -cp target/classes:target/dependency/* se.consys.silverkissen.heroku.Main
And if you're on Windows that'd be:
java -cp target\classes;target\dependency\* se.consys.silverkissen.heroku.Main
That aught to do it. This will execute your project with class-path's in the runtime. Assuming my limited knowledge of Java is correct.
Someone with more experience can probably explain in detail why this would work.
Open command prompt in the location where your jar/war located
And then run below command
java -jar silverkissen.war
I have the code on github,
https://github.com/Quillion/Game
And I would like to make it executable. jarIt.bat is the thing that makes the jar.
path C:\Program Files\Java\jdk1.7.0_04\bin;%path%
cls
javac *java
echo Main-Class: Main> manifest.txt
jar cvfm Main.jar manifest.txt *.class
java -jar Main.jar
del *.class
I can execute it on my computer using the run.bat file,
path C:\Program Files\Java\jdk1.7.0_04\bin;%path%
cls
java -jar Main.jar
but if I transfer it to another computer or Linux, I still have to use the commands to execute it. How does one create a jar file so that it can be double clicked and executed?
I am somewhat bad at the whole jar explanation, so if someone can even find a good link to share that explains everything I will be really grateful.
I have tried reading numerous explanations, but none explain how to do it properly. I know that I can accomplish this using eclipse, but I would like to learn the know how and to understand how to make one myself without any help of IDE.
Unfortunately, not all OS support running the jar by a double click ootb. You'd be off best by generating scripts for all environments that you need to support. You can do that with Maven and the plugin that does the scripting for you:
http://mojo.codehaus.org/appassembler/appassembler-maven-plugin/
Btw, your jar should already run by double click in a modern windows installation! You did it quite right.
I've got a weird problem that I can't understand... I have a simple HelloWorld jar that I built in Eclipse which has the Apache Loggings jar on it's classpath. I've written a script to run the jar:
#!/bin/sh
export CLASSPATH=lib/*:$CLASSPATH
java -jar HelloWorld.jar
The directory structure here is a main directory with the HelloWorld.jar and a lib subdirectory holding the commons-logging-1.1.1.jar.
Running this script works fine. However, when I place the HelloWorld.jar into the lib directory (i.e. to contain all the JARs in one place), and executing java -jar lib/HelloWorld.jar, I get:
Exception in thread "AWT-EventQueue-0" java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
error. Why?!?!?!?!
I'm asking this because I've used the JarBundler on OSX to create an Application bundle for the HelloWorld app and placed a modified script in the MacOS directory whereas all the libs are placed in the Resources/Java directory. Modified version of the script is:
#!/bin/sh
RESOURCE_DIR=$(cd "../Resources"; pwd)
export CLASSPATH=$RESOURCE_DIR/Java/*:$CLASSPATH
java -jar $RESOURCE_DIR/Java/HelloWorld.jar
and I'm getting the same error as above I'd really appreciate any help understanding why I can't do this and/or how to fix it?
Classpath doesn't work with wildcards. Every jar has to be specified explicitly, either as part of the CLASSPATH variable or in the manifest of another jar that is included in the classpath.
Also, IIRC java -jar ignores all the third party jars that are present in the classpath. Why not do this instead?
java -cp yourJar:logJars <mainClass>
Try to add the commons-logging-1.1.1.jar to the CLASSPATH directly
Java will not work with lib/* but the shell may be expanding it for you. Double check this. Put a line like this after export:
echo $CLASSPATH
Also, I would recommend putting it in the MANIFEST file as already mentioned.
EDIT:
Is it a permission problem? If you run the app as root/admin or put the file somewhere else and use a fully-qualified path does it work?
Use the MANIFEST file (META-INF folder) to deal with Classpath entries. Use relative paths for the libraries.
For further info, take a look here.
Basically, for the case with commons-logging inside a lib folder:
Class-Path: lib/commons-logging-1.1.1.jar
And for both jars in the same folder:
Class-Path: commons-logging-1.1.1.jar
Thanks to everyone for their help in figuring this out. Basically, a manifest file was being created and bundled into the jar without my knowledge so any $CLASSPATH or -cp flags were being ignored. In my Eclipse project, I had my classpath set to $(projectRoot)/lib which coincidentally the same directory structure as my dist directory. However, when they were bundled into one directory by OSX's JarBundler, the directory was no long present, hence the classpath errors!
I tried removing the Class-Path attribute from the MANIFEST.MF that Eclispe created but the command line $CLASSPATH and/or -cp entries still don't seem to make a difference... Does the existence of a manifest file negate all command line classpath entries?
have you set log4j.jar into your class. i think you didnt added log4j.jar of to its class path.
I've looked through many of the existing threads about this error, but still no luck. I'm not even trying to package a jar or use any third-party packaging tools. I'm simply running from within Eclipse (works great) and then trying to run the exact same app from the command line, in the same location it's built to (getting this error). My goal is to be able to zip up the bin folder and send it off to be run by someone else via a command line script. Some details:
It's a command-line app and I'm using the commons-lang-2.4.jar for string utilities. That is the file that cannot be located (specificaly "java.lang.NoClassDefFoundError: org/apache/commons/lang/StringEscapeUtils")
I have that jar in my lib folder and have added it to my build path in Eclipse via right-click "Build Path -> Add to Build Path"
The .classpath file looks correct and contains the reference to the jar, but I assume that file is only used by Eclipse (contains this line: <classpathentry kind="lib" path="lib/commons-lang-2.4.jar"/>)
Could this be related to the Eclipse working directory setting? I have some internal template files that I created that are under src/templates, and the only way I can seem to get those to be seen is by setting the project working directory to AppName/src. Maybe I should be putting those somewhere else?
Let me know if any additional info would help. Surely this is something simple, but I've wasted too much time on it at this point. This is reminding me why I originally left Java back in '05 or so...
A NoClassDefFoundError basically means that the class was there in the classpath during compiletime, but it is missing in the classpath during runtime.
In your case, when executing using java.exe from commandline, you need to specify the classpath in the -cp or -classpath argument. Or if it is a JAR file, then you need to specify it in the class-path entry of its MANIFEST.MF file.
The value of the argument/entry can be either absolute or relative file system paths to a folder containing all .class files or to an individual .jar file. You can separate paths using a semicolon ;. When a path contains spaces, you need to wrap the particular path with doublequotes ". Example:
java -cp .;c:/path/to/file.jar;"c:/spacy path/to/classes" mypackage.MyClass
To save the effort of typing and editing the argument in commandline everytime, use a .bat file.
Edit: I should have realized that you're using an Unix based operating system. The above examples are Windows-targeted. In the case of Unix like platforms you can follow the same rules, but you need to separate the paths using a colon : and instead of an eventual batch file, use a .sh file.
java -cp .:/path/to/file.jar:"/spacy path/to/classes" mypackage.MyClass
Are you specifying the classpath to java on the command line?
$ java -cp lib/commons-lang-2.4.jar your.main.Class
The classpath setting you are setting in Eclispe are only for the IDE and do not affect how you application is run outside the IDE. Even if you use the Eclipse Functionality to export your application as an executable jar file there is no out of the box way to package all the jars your application depends on.
If you have packaged you application into a jar file called myapp.jar then running a command like below will run the application with the jar you depend on, if you have more than one just add them separted by ; on Windows or : on Unix:
java -jar myapp.jar -cp .;c:/pathtolibs/commons-lang-2.4.jar
If you are just running the classes directly then either run the folder containing your .class files will also need to be on the path (though I assume it already is since you are able to run the program and get errors).
Consider File -> Export -> Runnable jar to create a jar file which can be invoked directly with
java -jar yourProgram.jar
There are several variants depending on your needs.
Eclipse does not move any of the jars in your classpath into the bin folder of your project. You need to copy the util jar into the bin folder. If you move it to the root of the bin folder, you might be able to get away without any classpath entries but it's not the recommended solution. See #BalusC's answer for good coverage of that.
Eclipse doesn't build executable java classes by default. Don't ask me why, but it probably has something to do with using their own tools.jar (somewhere in plugins/org.eclipse.core ?) so that Eclipse can run without a JDK.
You can usually go to your project bin directory and do:
java -cp . MyClass
But if you have external jars, Eclipse handles those internally in another weird way, so you'll need to add those too.
make sure your jar commons-lang-2.4.jar in classpath and not redudance.
I ever add jar file to my classpath, and have 2 file jar in my classpath. After I delete it, work smooth