When I run the Jar-file inside netbeans it works.
But after I clean and build it, it won't run.
It gives me an error, that the Mainclass couldn't be found.
I tried already something like:
java -cp C:\javaProjectsTests\J.jar;C:\javaProjectsTests\lib\*;. j.J
java -Djava.library.path=C:\javaProjectsTests\lib\* -jar C:\javaProjectsTests\J.jar
Maybe someone could help?
Please refer below link
Execute jar file with multiple classpath libraries from command prompt
Lod your jars to classpath and execute your class (with main method)
Thanks,
It was a syntaxerror in the path-string.
It works now, but only if the lib-folder(other jar-files) is in the same directory as the executing jar-file. Should not a problem for comandline programs, as it's possible to use a batch-file to execute from somewhere else.
However I asked me if it's possible to merge archives, as I could read on a website here ?
Related
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
Uploaded a jar file from my computer to a server and tried to run it. When I run it I get java.lang.NoClassDefFoundError and it seems to be related to the twitter4j jar that my main method depends on.
However, I have this jar file in my libraries so shouldn't this be included when I build my code in to a jar? Here's a pic in case it helps.
is the error that I'm getting. (can't upload a pic just yet.
Not sure what this has to do with twitter, but anyway, the issue is that you do not have the correct class files. In other words, when you are running your fat JAR in the command prompt, you do not have any libraries exported with it (Or if you do, you don't have that specific one).
Sometimes such an error can be because there is an incorrect version of java, however that is not the case here since java has got no "twitter" packages or classes in it.
Using something like JarSplice would fix this.
Assuming you did not package the twitter4j classes inside your application jar, you need to tell Java where it can look for classes that are not inside your application jar. You typically use the classpath flag for that. In your case, it should look something like
java -cp /tmp/twitter4j.jar -jar /tmp/myapp.jar
An alternative would be to package all twitter4j's classes inside your application jar. This is called a 'fat' jar. How to make one depends on how you build your application jar.
The JAR file that you are trying to use needs to be in the classpath. This can be done by using the -cp attribute from the command line. However, when using java -jar, you cannot use the -cp attribute.
To get around this, you can do the following:
java -cp /tmp/myapp.jar;\path\to\external.jar com.example.package.MyClass
where MyClass has the main() method defined.
Alternately, you can specify jar files on the classpath using the manifest.mf file. See http://docs.oracle.com/javase/tutorial/deployment/jar/manifestindex.html for details.
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.
How do I run use emma with a jar file. I downloaded emma.jar and another jar for testing purpose (MySQLConnector.jar). I placed both the jars in the same folder and tried to give this command
java -cp emma.jar emmarun -jar MySQLConnector.jar
. But I got an error saying
emmarun: failed to load Main-Class manifest attribute from
[C:\Documetings\Administrator\Desktop\jars\mysql-connector-java-5.0.8-bin.jar]
How do I solve this problem!!.. ANy other methods to test code coverage are also welcome!!
Thanks in advance!!
We only can instrument executable jars. Which makes sense: emma needs something to run. And the connector library simply isn't executable: it's manifest file does not have the Main-Class attribute and I guess, the library does not contain a class with a main method too.
Rethink what you really want to test. The coverage report will show which lines of code have been executed during a (test) run. It does not tell us, which lines are executable.
Find yourself an executable jar and then you should see valid result.
this will sound silly but i am executing my code from command prompt and have to use a jar in my class.
I have placed my jar in lib folder of JDK..
but i am still getting error of file not found
any explanation??
EDITED : guys tried all but still not working
EDIT 2 :i am trying to work as was told by this link i am using js-1.6R5.jar
Edit 3 : i undestand all the things you ppl have told but nothing working for me.. pls give me a link to upload my example that i can share with you all.
Edit 4 : i am fed up by setting classpaths but its not working... i have SDK installed in my system, do i need an extra JDK to run my programs from command prompt??
You need to add the jar to the class path by doing the following...
java -classpath D:\myprogram;D:\myprogram\lib\supportLib.jar org.mypackage.HelloWorld
Please see Wikipedia - Classpath_(Java)
You can place it anywhere, as long is you include it in your classpath. See Setting the Class Path for how to include jars in the classpath.
Have in mind that adding something in the JDK lib is almost never a good idea.
You can make a lib folder in your application's directory and put jar files there, then make your application find them by adding lib to your application's classpath.
And, don't put your jar files in JDK's lib folder. It's not good practise.
You need to let Java know that you want to include the jar in your classpath (the list of folders and jars it checks for classes). One way to do this is with the -cp command line argument, something like
java -cp ".;pathToMyJar\myJar.jar" MyClass
Another is to edit the CLASSPATH environment variable in your OS to include your jar.
A simple solution will be to place the jar fiel inside the windows folder if you are doing it in a Windows machine.
Unfortunately your question contains a lot of question signs and few information.
If you are using java.io.File to open jar as a regular file this jar should not be in lib directory. You just have to provide correct path in file system.
If however you are just trying to use jar as a part of your application it should be in classpath. Use either command line java -cp myjar.jar MyMainClassor put full path to this jar to global environment variableCLASSPATH`.