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.
Related
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
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 .
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
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.
In the tutorial I found out that jar files can be created in the following way:
jar cf jar-file input-file(s)
However, it was not clear what are the input-file(s). Is that .java files or .class files? From the examples on the same page I can assume that should be .class files.
But now it is not clear which .class files should I put there. After the compilation of .java files I have a lot of .class files. One of the reason of that is that I have a lot files like that: GameWindow$2$10class, GameWindow$2$7.class and so on. Should I include all of them into the command line for the creation of the .jar file?
To run my application I use java Game. So, my be I need to use only Game.class file when I create a .jar file? On the other hand other class files corresponds to classes used by the application.
My software use external libraries (during compilation I specify .jar files of these libraries). Will .jar file of my application be able to run on the computer which does not contain the .jar file of used library?
However, it was not clear what are the input-file(s). Is that .java files or .class files? From the examples on the same page I can assume that should be .class files.
Yes, you need to include the class files.
I have a lot files like that: GameWindow$2$10class, GameWindow$2$7.class and so on. Should I include all of them into the command line for the creation of the .jar file?
Yes, these are from inner classes; you need them as well.
To run my application I use java Game. So, my be I need to use only Game.class file when I create a .jar file?
No, class Game will use other classes, which in turn use others. You need them all.
Will .jar file of my application be able to run on the computer which does not contain the .jar file of used library?
No.
That said, creating a JAR manually is a good learning experience, but not something you'd really do in practice.
You should probably look into automating you JAR building. Check out e.g. Ant: http://ant.apache.org/
You can use a wild card to add the classes in the current directory into the JAR-file.
jar cf mynewjar.jar *.class
When you compile your source file into byte code, all classes inside that source will be generated as separate .class files, so unless your game.java has more than the Game class, the game class would be sufficient.
The Jar-file could contain any file you want, but to hold a Java-program you need at least the .class-files, so you have to include them. The Game-class you are talking about may be dependant on the other classes. You can check that: delete all .class-files and recompile only Game.java (javac Game.java). All other classes that are compiled are a dependency. All of these have to be included in the Jar-file, that your program can be run. The class-files generated, that have not a corresponding .java-file (i.e. your GameWindow$2$7.class) are inner anonymous classes, in your example a inner class of the class GameWindow. If other libraries are needed, these must be present on other computers, that your program can be run. But you can include the content of the other jars into your jar, so that all that is needed is bundled into one file.
The .jar file must contain all your classes that are needed during runtime. That includes the GameWindow$2$10 classes, they are the anonymous inner classes that you wrote in your GameWindow class.
Other .jar files are not included in your .jar file, but you can reference them using the Class-path attribute in your manifest.
regarding your external dependencies, this will NOT work unless you compile in the dependencies as .class files, or use something like FatJar http://fjep.sourceforge.net/ to add them into one big jar.
You can create a .jar file, zipping the folder with the compiled classes and renaming the file.
For instance, if your class files are located in the "game" folder, zip it to game.zip, and rename it to game.jar
This is really simple and it works!