classes of jar file cannot be imported in netbeans - java

I created a jar file from a bunch of java files. Folder structure was org/ax/redis. I used the command jar cvf jedis.jar org/*. Then I imported this jar file in my netbeans project. Then when I tried importing classes from it, by writing import org.ax.redis.*. However, netbeans shows error that no such package exists.
Now I opened another jar file of log4j to see how it is from inside. Only difference was in manifest file. It had a bunch of directives like Name: org/apache/log4j/. So I created a manifest file for my jar file by including Name: org/ax/redis/. Used this command to add manifest information in my jar jar cvfm jedis.jar META-INF/manifest.txt org/*. Still nothing works. Please help me

Jar files typically contain class files (the results of compilation) rather than source files (*.java). While some jar files may contain both, only the class files are available to compile or run against (e.g. using -cp library.jar).
So basically, before you build your jar file, you need to compile your code - and then include the class files in the jar file. If you include the source files as well (in the same directory structure) then some IDEs may be able to detect that, which can be useful.

Even If u don't generate .class file , the package still can be imported and it won't show any compilation error(If U don't specify any particular class) . However when U try to use a particular class function of that package , it will throw an error .

Related

Class files (.java) inaccessable from Jar

I created a Jar file.
When I use it in my project it seems to be able to find the Package name just fine but I can't use the classes within.
Package name, but no classes after.
Did I do something wrong when creating the Jar?
I ran "jar cvf Adapter.jar *".
Any suggestions would be a great help!
A Java JAR file is supposed to contain compiled .class files, not .java source files.
You'll need to compile your files using javac and then use jar on the output.
As NimrodArgov pointed out, your IDE (apparently) can't find the class because it does not exist as compiled .class file as it would be expected. The JAR package only contains the Java source files.
As far as I can see, there is no need for you to pack your classes into a .jar file either. Just make sure the containing folder structure is in your Java Classpath.

Jar file when extracted and rebuilt, isn't working

I'll try to be clear and concise on this.
i have a jar file of a project which is working perfectly. Now I need to make some changes to the application so i extracted that jar using this command.
jar xf jwire.jar
the files extracted are
META-INF (containing manifest file)
Resources (containing pictures used in application)
Jwire (containg classes and java files)
.classpath
.project
manifest.mf (another manifest file with some other path to main class)
i also added two empty folders src and bin here on somebodys suggestion.
now i'm trying to create the jar file again using all these folders.
Without making any type of changes I'm trying to rebuild this jar using the following
jar cf newjar.jar G:\Project1\project
NOTE:- I am guessing it's the right way to build the file. Please point if it's not right.
G:\Project1\project is the path to above mentioned files and folders.
The jar created using this is of size 729kb while the earlier one had 711Kb and when I run it using the following command.
java -jar newjwire.jar
I get the following message
no main manifest attribute, in newjwire.jar
Next i tried extracting this newjwire.jar file in order to see its manifest file. I got this folder META-INF containing the MANIFEST.MF file. it didn't have the path to the main class , So I edited it and put the modified file back on using the following command
jar uf newjwire.jar META-INF/MANIFEST.MF
when I run this jar, I get the following message
Error: Ivalid or corrupt jarfile newjwire.jar
What am I doing wrong? Am I building the jar file the right way? I used eclipse juno too and it gave the same message to that mainclass is missing. There is a main class consisting the main function. Is this something has to do with My orignal jar having two MANIFEST file.
I hope the problem is understandable. Clear and detailed answers will be appreciated.
you're supposed to update JAR files by jar uf jar-file input-file(s)
see this link: http://docs.oracle.com/javase/tutorial/deployment/jar/update.html
According to this page you need to EITHER have Manifest.txt present when you create or update the JAR, or use the e option to specify the main class. Probably the latter would make more sense in your case.
jar cfe newjar.jar mypackage.MainClass G:\Project1\project

how to include external jar files while creating a jar file in linux?

I have an external jar file mysql-connector-java-5.1.26 under /Users/remy/Downloads/mysql-connector-java-5.1.26/mysql-connector-java-5.1.26.jar . I want to create a jar file from hello.java which should include the external jar file as specified as well, where hello.java is dependent on the external jar file.
Users
-->remy
-->Downloads
-->mysql-connector-java-5.1.26
-->mysql-connector-java-5.1.26.jar
Users
-->remy
-->Documents
-->Hello.java
How to create the myManifest.txt and include the external jar file so as to compile and run them together?
There are two ways.
Create a MANIFEST.MF that refers to the other jar in its classpath
Whenever you run the program, make sure you include both jars in your classpath.
That said, I think you should go back and read a tutorial because you've made a few mistakes in your question.
"hello.java" is not a jar file as you've said. This is a java source file. It will be compiled into a class file. You can take this class file and put it in a jar file.
You can't name your manifest file "myManifest.txt" AFAIK. It has to be named something specific and be located in a specific place. Read a tutorial to see the details on that.

can't import jar file

I had created a jar file with three classes using this cmd line:
C:\...\db>jar cvf views.jar Line.java Points.java Shapes.java
I can add the jar file to IDE but I can't import it to the code.
Another thing is the classes in the jar file are xxx.java, but when looking in another jar file i noticed that the classes are xxx.class
I don't know if that is the problem or not.
Jar is nothing but archiving (zipping)
You are clearly zipping the .java files to your jar file.
First Compile your .java files using javac
than issue your jar command on the generated .class files
Refer How to Create Jar
You should refer to .class files when creating the JAR file instead of the source .java file.
Here you can find a tutorial about JAR creating from Oracle official site.
jar is an archive tool which is just packing your compiled java files. This means that you should compile those .java files first, and than add generated .class files in jar.
I would normally expect a .jar file to contain classes rather than .java files (or at least the source could be alongside the classes)
You can zip anything into a .jar file (it's a .zip by another name - see here for more info) but I suspect the IDE is expecting .class files. Note that my IDE (Intellij) allows me to specify a jar/zip containing source, but that's usually alongside a jar containing the compiled code.
You package java source files instead of compiled class files in the jar. If you want you jar to be used in IDE you should package class files. Something like
C:\...\db>javac Line.java Points.java Shapes.java
and then
C:\...\db>jar cvf views.jar Line.class Points.class Shapes.class

How can I specify dependencies in the manifest file and then to include it into my .jar file?

I generated .class files by the following command:
javac -cp \directoryName\external.jar myPackageDirectory\First.java myPackageDirectory\Second.java
I needed to use -cp during compilation and name of .jar file of an "external" library (external.jar) to be able to use this library from my code.
Using my .class files I have generated my .jar file in the following way:
jar cfm app.jar manifest.txt myPackageDirectory\*.class
manifest.txt contains just one line:
Main-Class: myPackageName.First
My problem is that I am not sure that I will be able to run my .jar file on other computers. I think so because during the compilation I specified the location of the .jar file of the external library. So, my .class files (included into the .jar file will try to find the .jar file of the external library in a specific directory and there is no guaranty that that the .jar file of the external library will be in the same directory as on the my computer.
I heard that the above problem can be solved by a
usage of a MANIFEST file that I
include in my own jar, and which will
list dependency locations
but I do not understand how it works. I do need to specify location of the "external.jar" at the compilation stage (otherwise the compiler complains).
First of all: you don't seem to compile a class called MainClass and all your .java files seem to be in a package, so I assume that MainClass is just a placeholder and you actually use the correct class name here.
You need to specify a Class-Path header that mentions your external .jar to your manifest.txt and deliver the .jar file together with your jar. You need to do this in addition to specifying the -cp at compile time.
Further to what Joachim Sauer (very correctly) says, there is a way to pack your dependency jars into the same jar as your own code. The programs that accomplish this create a super-main class and manipulate the classpath to find the dependent jars in your resulting jar.
Several programs can do this; one of them is called OneJar.

Categories