i have prepared a java file that uses Json library so i downloaded a JSON library from "http://code.google.com/p/json-simple/downloads/detail?name=json_simple-1.1-all.zip&can=2&q="
but very confuse where to place it, so that my java file can detect Json library.
I tried to place the lib "json_simple-1.1" inside "C:\Program Files\Java\jdk1.6.0_07\lib"
but not working yet.
How can i solve the problem using environment variable or any other means where i don't need to set path on comman prompt?
There are number of ways you can include jar file to run your program.
java -classpath "{yourpath}/json.jar:." my.package.Program
java -cp "{yourpath}/json.jar:." my.package.Program
Other way is to set env variable java.ext.dirs.
-Djava.ext.dirs=jarDirectory
You can put it beside your jar (in case you package your application into a jar) and add a link to it in your manifest, e.g.:
your jar contains a META-INF/MANIFEST.MF file which has (besides others) this entry:
Class-Path: json_simple-1.1.jar
More details here.
Try the following:
set CLASSPATH=%CLASSPATH%;<path to libraries folder>
Related
I have to implement upload resume feature in my project.I am making Java project using textpad .For uploading ,I have to import org.apache.commons.io jar file.Before running project, I have to set path of org.apache.commons.io at cmd prompt which I have placed in a folder in my project.I have to set path every time I run the project.Is any other solution so that I can get rid of this problem.I am new Java programmer.Suggestions and help is appreciated. Thanks!
You can set (or append) system property CLASSPATH to include your commons.io jar, so that the jar is automatically included whenever you run your "java" command.
On Linux:
export CLASSPATH=$CLASSPATH:/usr/local/lib/commons-io-2.4.jar
On Windows:
see http://www.computerhope.com/issues/ch000549.htm
More details on using the CLASSPATH system variable can be found here http://docs.oracle.com/javase/tutorial/essential/environment/paths.html or here http://javarevisited.blogspot.fi/2011/01/how-classpath-work-in-java.html
Adding jar to our java code can be done using following command:
SET JRE_HOME=<path to your jre>
%JRE_HOME%\bin\java.exe -jar apache.commons.io.jar
Here you need to mention jre home and need to set path for the jar you want to add is.
Is it possible to take existing .class files and a MANIFEST.MF to create a jar file?
Is there a library that can create a "valid" jar-file? I tried it manually and it didn't work (using 7zip).
ERROR: "Invalid or corrupt jar file"
If everything has been compiled before, it should (in my understanding) theoretically work, if you create a new zip file, put all the files in it in the original structure and then rename it to "jar".
My idea is to program something like this with java code. A solution where I could add a file to an existing jar, would also be ok.
If you're interested in why I want to use this, look at my initial question: Compile javacode out of a running java accpilaction - on a system that hasn't JDK installed
Well Jar -cf
Try the jar command in $JAVA_HOME/bin
$JAVA_HOME is the path to you JRE/JDK installation
I m running a java program from a batch file which refences some external jar files .How do i include those jar files in my batch file.Please help
Look at the Sun's official documentation: Setting the class path to understand your options.
A quick way would be just to include your JAR(s) after the -cp:
on Windows
java -cp C:\java\MyClasses\myclasses.jar;C:\java\MyClasses\myclassesAnother.jar utility.myapp.Cool
on Linux/Unix
java -cp /opt/thirdparty/myclasses.jar:/opt/thirdparty/myclassesAnother.jar utility.myapp.Cool
You need to set classpath http://download.oracle.com/javase/1.3/docs/tooldocs/win32/classpath.html. E.g.,
java -cp file1.jar;file2.jar yourApp
or if your jar-files are located in directory lib/
java -cp lib/* yourApp
Not to bother with -classpath parameter you could put references to the jar files into the manifest.mf of your application JAR, if it's you application of course.
Adding Classes to the JAR File's Classpath
Sorry, I dont know others IDE, but works with me on Eclipse.
Right click on your project, select
Properties/ Java Build Path/ Libraries/ Classpath/ Add External JARs...
Then choose whatever files you need :v
You have to fill the Class-Path parameter of the manifest file of the JAR. The standard documentation explains that very well.
When running a jar or class file specify classpath option
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
I want to simplify my execution of a Groovy script that makes calls to an Oracle database. How do I add the ojdbc jar to the default classpath so that I can run:
groovy RunScript.groovy
instead of:
groovy -cp ojdbc5.jar RunScript.groovy
Summarized from Groovy Recipes, by Scott Davis, Automatically Including JARs in the ./groovy/lib Directory:
Create .groovy/lib in your login directory
Uncomment the following line in ${GROOVY_HOME}/conf/groovy-starter.conf
load !{user.home}/.groovy/lib/*.jar
Copy the jars you want included to .groovy/lib
It appears that for Groovy 1.5 or later you get this by default (no need to edit the conf), just drop the jars in the /lib dir.
There are a few ways to do it. You can add the jar to your system's CLASSPATH variable. You can create a directory called .groovy/lib in your home directory and put the jar in there. It will be automatically added to your classpath at runtime. Or, you can do it in code:
this.class.classLoader.rootLoader.addURL(new URL("file:///path to file"))
One way would be using #Grab in the code:
#GrabConfig(systemClassLoader=true)
#Grab('com.oracle:ojdbc6:12.1.0.2.0')
Class.forName("oracle.jdbc.OracleDriver").newInstance()
groovy is just a wrapper script for the Groovy JAR that sets up the Java classpath. You could modify that script to add the path to your own JAR, as well, I suppose.
You could add the following shebang to the first line of your Groovy script:
#!/usr/bin/env groovy -cp ojdbc5.jar
Then, mark the script executable:
chmod u+x RunScript.groovy
Now, running the script by itself will set the classpath automatically.
./RunScript.groovy