Running jar file from cmd with reference to external jars - java

I would like to know can I run a jar file from the command, with the jar file using log4j and ojdbc.jar as well.
The 'main' is located in: nmap_logic.jar.
Within the package containing the 'main' is called: "nn.gmap.logic".
I also use 2 external jar files: log4j.jar & ojdbc.jar.
I have tried running:
java -cp "nmap_logic.jar;log4j.jar;ojdbc.jar" nn.gmap.logic.NNmain
And I get an error that the log4j cannot be initialized.
From the Eclipse environment the application runs fine.
Please let me know how should I execute the command properly.
Thanks.

Try to give the full path to the jars. I believe that there is a difference between what you think is your root folder and what Java thinks about it.
Something like java -cp "c:\myjars\nmap_logic.jar;c:\myjars\log4j.jar;c:\myjars\ojdbc.jar" nn.gmap.logic.NNmain
Btw, you can also do the following: java -cp "c:\myjars\*" nn.gmap.logic.NNmain

Related

How to refer to a properties file in a batch file

I'm trying to run my Java program using a batch file, I'm able to run it properly. However, when I run the batch file after inserting the code to read a properties file from the Java program, I'm getting the following error.
Can't find bundle for base name app1, locale en_US
Actually i have a conf folder under which I have this properties folder, then I came to know that I need to keep this conf folder in the class path. But I have actually added it as class folder using Eclipse. However, I'm getting the same error. Please let me know what exactly I need to do for running the Java program using batch file. Using Eclipse I'm able to run the properly.
Thanks,
Balaji.
From the comments your batch script:
#echo off
java -Djava.ext.dirs=lib -classpath ./bin com.myapp.app1.demoprogram
pause
Notice how the /conf/ directory is not listed on the classpath. The easiest way to get it there is to just add it to the -classpath argument being passed to the JVM. Something closer to:
#echo off
java -Djava.ext.dirs=lib -classpath ./bin;./conf com.myapp.app1.demoprogram
pause
This is assuming that /conf/ is in the same directory as bin. You might have to do some tweaking to get the setup to work for you application, but the root problem is that while you added the /conf/ folder to the project classpath in eclipse, you need to do the same thing for the batch script so the JVM can find it

java.lang.NoClassDefFoundError. Class not found during runtime.

I am working on eclipse, and I have the need to use external library's. For example Jsoup and JXL.
Now what I have done so far is: First created a "lib" folder in my project folder. Afterwards in eclipse, click on project properties, Libraries tab, add external jar and added the jar in the lib folder.
So this solve my compilation issue. Now, when I run the program (I go to project/bin and in the console execute: java ProgramName ; I get
java.lang.NoClassDefFoundError:
Now to testing, I added the Jar file to the folder where Main.java is and Now, I have been able to run the program doing the following:
javac -classpath ./path/to/jar Main.java
java -classpath ./path/to/jar:. Main
And this works.
So the first thing that comes to mind is that I have to tell java where to find the respective libraries. If this is correct? How do I do it?
java -cp ???(dont know what to put here)
But moreover. I have another issue. I am writing this program in a computer, but I am going to use it in other which probably don't have those libraries. How do I solve this issue?
I like to use something like the following:
java -cp myjar.jar;lib/*.jar com.foo.bar.MyClass
This adds not only my jar to the classpath but those in the lib directory as well.
If you want to run your jar on another computer, you will need those jars as well, you cant just have your jar. Why not just also package your lib directory along with it?
To get your program to run you have two paths to worry about
The path to the jar files that are your applications dependencies (like jsoup.jar) (lets call this lib)
The path to the directory containing the classes of your app (lets call this classes)
The general form of the command line you need is:
java -cp lib/jsoup.jar:classes Main
If you have more libs
java -cp lib/jsoup.jar:lib/jxl.jar:classes Main
A general note on packaging your app for release to other computers. You might want to consider making a jar of your own app, probably best done using http://ant.apache.org/manual/Tasks/jar.html
Another option is to produce a "one jar", which makes one large jar, bundling in all the classes you need from your libs and all the classes in your app. You can then make the jar executable for a nice out of the box solution. Have a look at http://one-jar.sourceforge.net/ and https://code.google.com/p/jarjar/
if you have this structure:
project folder
... code
... libs
then from the code folder:
javac -cp .;../libs/*.jar yourmainclass.java
java -cp .;../libs/*.jar yourmainclass
When you need to compile and run this project, take all the folder and do the same in other machine.

Run an execution with several jars

How can I run a jar that needs several jars to work?
Let me explain, I have for example a project with a jar "Main.jar" but to run this Main.jar I need jdom.jar(for xml file), jGit.jar...
Assume we need more than two jars. How can I run my Main.jar?
By including the needed jar files in the classpath. Something like:
java -cp "Main.jar;jdom.jar;jdom.jar" MainClass
If you are under Windows and would like to execute the Main.jar with a double click, you will need to create a .bat file and use that one instead to run your program. The content of the .bat file will have the above command.
Under Unix/Linux you will create a shell file with the similar content.
Note that the -cp argument values will need to contain all the jars that your Main.jar is depended on.
Run the jar with main class and add all other jars to classpath.
java -cp yourJars yourClass
see this post for more info. See this java tutorial

Java - Difficulty installing program from 3 separate .jar files (involves CLASSPATH)

I'm having a little trouble running some Java code, which requires three .jar files to be used. I'm at a lost as to what to do with them--I've tried setting the CLASSPATH (and following the instructions for how to do so in the readme files), but to no avail.
I was wondering if someone could walk me through it? I'd imagine three .jar files would be an easy install for someone who knows what they're doing.
If it helps, I'm using Ubuntu pretty much right out of the box (but I do have JDK and Eclipse installed!)
Runtime library: http://cogcomp.cs.illinois.edu/download/software/20
Additional .jar needed: http://cogcomp.cs.illinois.edu/download/software/23
Program I ultimately need to run: http://cogcomp.cs.illinois.edu/download/software/26
If you're willing to help, I can't thank you enough--you deserve a million kudos!
G
Those are all JAR files. When you execute a JAR file by doubleclicking or using java -jar, the CLASSPATH environment variable and the -cp and -classpath arguments are ignored. The classpath should be defninied in META-INF/MANIFEST.MF file of the JAR. In this particular case, only the second and third JAR have a Class-Path entry in the manifest file:
Class-Path: LBJ2Library.jar
Which is the first JAR. The classpath is telling that it is expecting the LBJ2Library.jar to be in the same folder as the JAR you'd like to execute (either the second or third one).
So, just drop them all in the same folder and execute by java -jar LBJPOS.jar.
If you are using java -jar to run your jar files, then the CLASSPATH variable is ignored. If you are using java -jar, you have two options:
Combine the three jars into one jar.
Run the main class directory and don't use -jar.
Use of the CLASSPATH environment variable is generally discouraged nowadays. This is how it's done (on Linux):
java -cp library1.jar:library2.jar:mainapp.jar <fully qualified name of main class>
You need to set the CLASSPATH .place all the 3 jars in a folder , name it as lib
See below to set classpath
set CLASSPATH=%CLASSPATH%:lib;

Why has it failed to load main-class manifest attribute from a JAR file?

I have created a JAR file in this way jar cf jar-file input-files. Now, I'm trying to run it. Running it does not work (jre command is not found):
jre -cp app.jar MainClass
This does not work either:
java -jar main.jar
(Failed to load Main-Class manifest attribute from main.jar).
I also found out that
To run an application packaged as a
JAR file (version 1.2 -- requires
Main-Class manifest header)
What is the "Main-Class manifest header"? How do I create it and where do I put it?
I'm not sure I believe your symptoms:
If the jre command isn't found, then running jre -cp app.jar should give the same error
Just adding a JAR file to the classpath shouldn't give the error you're seeing
I'd expect you to see this error if you run:
java -jar app.jar
The Main-Class header needs to be in the manifest for the JAR file - this is metadata about things like other required libraries. See the Sun documentation for how to create an appropriate manifest. Basically you need to create a text file which includes a line like this:
Main-Class: MainClass
Then run
jar cfm app.jar manifest.txt *.class
set the classpath and compile
javac -classpath "C:\Program Files\Java\jdk1.6.0_updateVersion\tools.jar" yourApp.java
create manifest.txt
Main-Class: yourApp newline
create yourApp.jar
jar cvf0m yourApp.jar manifest.txt yourApp.class
run yourApp.jar
java -jar yourApp.jar
You can run with:
java -cp .;app.jar package.MainClass
It works for me if there is no manifest in the JAR file.
I got this error, and it was because I had the arguments in the wrong order:
CORRECT
java maui.main.Examples tagging -jar maui-1.0.jar
WRONG
java -jar maui-1.0.jar maui.main.Examples tagging
The easiest way to be sure that you have created the runnable JAR file correctly, with the appropriate manifest file, is to use Eclipse to build it for you. In your Eclipse project, you basically just select File/Export from the menu, and follow the prompts.
That way, you can be sure that your JAR file is correct and will know to look elsewhere if there is still an issue. The process is described in full in FAQ How do I create an executable JAR file for a stand-alone SWT program?.
I was getting the same error when i ran:
jar cvfm test.jar Test.class Manifest.txt
What resolved it was this:
jar cvfm test.jar Manifest.txt Test.class
My manifest has the entry point as given in oracle docs (make sure there is a new line character at the end of the file):
Main-Class: Test
Try
java -cp .:mail-1.4.1.jar JavaxMailHTML
no need to have manifest file.
I discovered that I was also having this error in NetBeans.
I hope the following is helpful.
Make sure that when you go to Project Configuration you set the main class you intend for running.
Do a Build or Clean Build
Place the jar file where you wish and try: java -jar "YourProject.jar" again at the command line.
This was the problem I was getting because I had other "test" programs I was using in NetBeans and I had to make sure the Main Class under the Run portion of the Project configuration was set correctly.
many blessings,
John P
I faced the same problem. This unix command is not able to find the main class. This is because the runtime and compile time JDK versions are different. Make the jar through eclipse after changing the java compiler version. The following link helped me.
http://crunchify.com/exception-in-thread-main-java-lang-unsupportedclassversionerror-comcrunchifymain-unsupported-major-minor-version-51-0/
Try running the jar created after this step and then execute it
If your class path is fully specified in manifest,
maybe you need the last version of java runtime environment.
My problem fixed when i reinstalled the jre 8.
If you using eclipse, try below:
1. Right click on the project -> select Export
2. Select Runnable Jar file in the select an export destination
3. Enter jar's name and Select "Package required ... " (second radio button) -> Finish
Hope this helps...!

Categories