can't import jar file - java

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

Related

how do i replace a jar present in tomcat lib directory with a directory consisting of all the .java files?

my tomcat lib directory consists of a jar which i want to replace with a directory containing all the .java files contained in the above mentioned jar.
Will addition of that directory to classpath in setenv.sh solve the problem?
eg PATH=$CLASSPATH:$XYZ/folder/*
Thanks in advance.
The jar file consists of all already compiled and packed up files. If you want to get the .Java files consisted in the jar file, you could decompile them, but it's not the best solution. You should rather find the source of the library (the jar file) or ask the maker of the .jar file to share it with you. There is no easy method of decompiling the .class files (consisted in the .jar file). It is possible, and you will get working .Java files, but the code will have a lot of strange blocks and things.
I believe you need to have *.class files instead of *.java

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.

Java .class files vs. .java file in a jar

Should you use .class files or .java files in a .jar library?
I want to take a class that I've written in a .java file and add it to a jar so I can use it as a library in my tomcat's WEB-INF/lib directory.
I understand that jars can contain .class files, .java files, or both (or just about anything else). I'm wondering what is specifically needed at runtime for my jar to be readable by the JVM. The answer to this question told me that .class files will work, but will .java files work as well?
I've found many descriptions of how to create a jar, and lots of information on the differences between .java files and .class files. What I'm lacking, I guess, is an understanding of how the jar libraries are interpreted by the JVM. Are they compiled at runtime?
Thank you
Jar files are collections of compiled java classes. You need the .class file in a jar. The .java file is you source file.
No. You'll need the byte-code (or class files) for the JVM to load the classes (although you can include the .java files, they won't be executed by the Java Runtime). A jar file is a zip file (with a tiny bit of metadata).
Also, you can add classes to your WEB-INF/classes folder (in addition, well instead of, to the WEB-INF/lib for jars, as you mentioned).

classes of jar file cannot be imported in netbeans

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 .

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