Java, using class files from a .jar - java

so i am trying to use classes from a jar file i downloaded. The jar file is not in the same directory as the .java file that will use classes from the jar file.
I run
jar -tvf json-simple-1.1.1.jar
and I get
0 Sun Feb 19 14:30:56 CST 2012 org/
0 Sun Feb 19 14:30:56 CST 2012 org/json/
0 Sun Feb 19 14:30:56 CST 2012 org/json/simple/
3939 Sun Feb 19 14:30:54 CST 2012 org/json/simple/ItemList.class
2077 Sun Feb 19 14:30:54 CST 2012 org/json/simple/JSONArray.class
161 Sun Feb 19 14:30:54 CST 2012 org/json/simple/JSONAware.class
3076 Sun Feb 19 14:30:54 CST 2012org/json/simple/JSONObject.class
223 Sun Feb 19 14:30:54 CST 2012 org/json/simple/JSONStreamAware.class
3964 Sun Feb 19 14:30:54 CST 2012 org/json/simple/JSONValue.class
0 Sun Feb 19 14:30:56 CST 2012 org/json/simple/parser/
I want to implement ItemList.class, and I now know that it is in the package
org/json/simple/
so in my .java file I do
import org.json.simple.ItemList;
public class Foo(){
public static void main(String[]args){
ItemList i = new ItemList();
}
}
I now need to compile Foo() so I run
javac -cp '.;json-simple-1.1.1.jar' Foo.java
I get the error that the package org.json.simple does not exist but in github the class ItemList has package org.json.simple
Am I missing something? Thanks in advance

If the jar is not in the same directory you need to give the path to the jar directory when specifying the class path.

Related

Why is my jar so big?

I've got a relatively simple jar-with-dependencies being built with Maven that is way larger than it seems like it should be. It's around 20MB, and in order to figure out what's taking up so much space, I've done the following:
First, I ran mvn dependency:tree. Then I checked in my .m2 cache for the size of each of the jars in the dependency tree. If I add up all of those sizes, it comes to about 8MB. How can I figure out where the other 12MB are coming from?
One thing I noticed in looking through my .m2 was that for many of the dependencies, they'll have something like this:
total 5224
-rw-r--r-- 1 user 289B Jul 25 2016 _remote.repositories
-rw-r--r-- 1 user 1.7M Jul 25 2016 commons-compress-1.12-javadoc.jar
-rw-r--r-- 1 user 407B Jul 25 2016 commons-compress-1.12-javadoc.jar.lastUpdated
-rw-r--r-- 1 user 40B Jul 25 2016 commons-compress-1.12-javadoc.jar.sha1
-rw-r--r-- 1 user 427K Jul 25 2016 commons-compress-1.12-sources.jar
-rw-r--r-- 1 user 407B Jul 25 2016 commons-compress-1.12-sources.jar.lastUpdated
-rw-r--r-- 1 user 40B Jul 25 2016 commons-compress-1.12-sources.jar.sha1
-rw-r--r-- 1 user 432K Jul 22 2016 commons-compress-1.12.jar
-rw-r--r-- 1 user 407B Jul 22 2016 commons-compress-1.12.jar.lastUpdated
-rw-r--r-- 1 user 40B Jul 22 2016 commons-compress-1.12.jar.sha1
-rw-r--r-- 1 user 13K Jul 22 2016 commons-compress-1.12.pom
-rw-r--r-- 1 user 407B Jul 22 2016 commons-compress-1.12.pom.lastUpdated
-rw-r--r-- 1 user 40B Jul 22 2016 commons-compress-1.12.pom.sha1
What are the -sources and -javadoc jars? Are those included in my uber jar? Because if every one of my dependencies uses the -javadoc jar instead of the standard one, that gets me a lot closer to 20MB.
Run
jar tvvf <your_simple-jar-with-dependencies.jar>
Or, open it with any zip compatible archiver and examine the contents to determine what is being included. Alternatively, run maven with -X for more extensive runtime information.

JFreeChart import error

Anyone can tell me where can I find the full version of JFreeChart? I downloaded the JCommon and JFreeChart as well (from the sourceforge page), but the JFreeChart contains only the jfreechart-demo.jar, so even if I correctly added the libraries in Eclipse, it doesn't work either...
Many Thanks!
When unpacked, the required JAR files are in the lib directory.
$ ls -ln lib
total 5800
-rw-r--r--# 1 501 20 45024 Jul 31 2014 hamcrest-core-1.3.jar
-rw-r--r--# 1 501 20 330246 Jul 31 2014 jcommon-1.0.23.jar
-rw-r--r--# 1 501 20 13796 Jul 31 2014 jfreechart-1.0.19-experimental.jar
-rw-r--r--# 1 501 20 79793 Jul 31 2014 jfreechart-1.0.19-swt.jar
-rw-r--r--# 1 501 20 1570157 Jul 31 2014 jfreechart-1.0.19.jar
-rw-r--r--# 1 501 20 50769 Jul 31 2014 jfreesvg-2.0.jar
-rw-r--r--# 1 501 20 245039 Jul 31 2014 junit-4.11.jar
-rw-r--r--# 1 501 20 445025 Jul 31 2014 orsoncharts-1.4-eval-nofx.jar
-rw-r--r--# 1 501 20 72444 Jul 31 2014 orsonpdf-1.6-eval.jar
-rw-r--r--# 1 501 20 80054 Jul 31 2014 servlet.jar
-rw-r--r--# 1 501 20 17492 Jul 31 2014 swtgraphics2d.jar
For example, the following command line runs TimeSeriesChartDemo1 on Unix; separate class path entries with a semicolon (;) on Windows.
$ java -cp .:lib/* org.jfree.chart.demo.TimeSeriesChartDemo1

Signing Jar files

I have purchased a code signing certificate for signing my jar files. After adding the whole chain of trust to the keystore, signing and validating the jar file I get this output:
sm 905 Tue Jul 22 15:48:42 CEST 2014 com/siemens/util/OSLogin$1.class
sm 881 Tue Jul 22 15:48:42 CEST 2014 com/siemens/util/OSLogin$2.class
sm 1802 Tue Jul 22 15:48:42 CEST 2014 com/siemens/util/OSLogin.class
s 434 Tue Jul 22 15:48:42 CEST 2014 META-INF/MANIFEST.MF
444 Tue Jul 22 15:48:42 CEST 2014 META-INF/_B8D178A3-25F1-4475-8D11-C37
A786279F4_.SF
6413 Tue Jul 22 15:48:42 CEST 2014 META-INF/_B8D178A3-25F1-4475-8D11-C37
A786279F4_.RSA
s = signature was verified
m = entry is listed in manifest
k = at least one certificate was found in keystore
i = at least one certificate was found in identity scope
jar verified.
Warning:
This jar contains signatures that does not include a timestamp. Without a timest
amp, users may not be able to validate this jar after the signer certificate's e
xpiration date (2017-07-21) or after any future revocation date.
But after copying the jar to the web server and opening the webpage on the same system I get "The certificate is not valid..."
Did I miss something? I'm new to code signing.
Regards
mannson

How date and time is handled inside WAR/ZIP?

The very same file inside the very same WAR file is displayed by jar tvf myfile.war as modified at different times at Linux ad Windows.
Linux:
ubuntu#alex2:~/trydeploy2$ jar tvf mywar.war | grep custom.css
23932 Thu Jan 05 02:03:04 UTC 2012 _diffs_classic/css/.sass-cache/custom.css
7580 Thu Jan 05 02:03:04 UTC 2012 _diffs_classic/css/custom.css
25817 Tue May 28 00:18:40 UTC 2013 css/.sass-cache/custom.css
7693 Tue May 28 00:18:40 UTC 2013 css/custom.css
Windows:
D:\>"C:\Program Files (x86)\Java\jdk1.6.0_25\bin\jar.exe" tvf mywar.war | grep custom.css
23932 Thu Jan 05 02:03:04 MSK 2012 _diffs_classic/css/.sass-cache/custom.css
7580 Thu Jan 05 02:03:04 MSK 2012 _diffs_classic/css/custom.css
25817 Tue May 28 00:18:40 MSD 2013 css/.sass-cache/custom.css
7693 Tue May 28 00:18:40 MSD 2013 css/custom.css
Last file is of interest.
As you see the time component values are coincide but timezones differ.
Machines are really at different timezones.
How to handle this situation? What information is stored inside ZIP and where a mistake can be? Finally I need Linux machine to know modification times correctly.

Error with image resources in jar file

I'm having a problem with running a jar file. If I compile the code in eclipse everything is ok, but when I export to jar and try to run the program it throws an error.
The error is:
Exception in thread "main" java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(Unknown Source)
at game.ChessSquare.<init>(ChessSquare.java:17)
at game.ChessFrame.DrawField(ChessFrame.java:131)
at game.ChessFrame.<init>(ChessFrame.java:38)
at game.Chess.main(Chess.java:7)
So the piece of code that throws this error is
//text is something like "pawn" or "king".The whole image is an icon on the button
img = new ImageIcon(getClass().getResource("white/"+text+".png"));
As I understand, it can't find the image so img variable is null. But I can't understand why then it does run in eclipse. Any help with this? Thanks
P.S #AndrewThompson
E:\Desktop>jar tvf chess.jar
49 Sat Feb 11 23:40:06 GMT 2012 META-INF/MANIFEST.MF
378 Sat Feb 11 22:51:04 GMT 2012 game/ChessFieldCoord.class
1932 Sat Feb 11 23:39:48 GMT 2012 game/ChessSquare.class
536 Sat Feb 11 22:51:04 GMT 2012 game/Chess.class
2122 Sat Feb 11 22:51:04 GMT 2012 game/Rook.class
2357 Sat Feb 11 22:51:04 GMT 2012 game/Bishop.class
1946 Sat Feb 11 22:51:04 GMT 2012 game/Queen.class
2127 Sat Feb 11 22:51:04 GMT 2012 game/Pawn.class
1630 Sat Feb 11 22:51:04 GMT 2012 game/Knight.class
878 Sat Feb 11 22:51:04 GMT 2012 game/Empty.class
1642 Sat Feb 11 22:51:04 GMT 2012 game/King.class
915 Sat Feb 11 22:52:32 GMT 2012 game/ChessFrame$1.class
841 Sat Feb 11 22:52:32 GMT 2012 game/ChessFrame$2.class
695 Sat Feb 11 22:52:32 GMT 2012 game/ChessFrame$3.class
3525 Sat Feb 11 22:52:32 GMT 2012 game/ChessFrame$Move.class
4675 Sat Feb 11 22:52:32 GMT 2012 game/ChessFrame.class
997 Sat Feb 11 22:51:04 GMT 2012 game/ChessFigure.class
928 Sat Feb 11 15:49:32 GMT 2012 game/black/bishop.png
1947 Sat Feb 11 15:49:10 GMT 2012 game/black/king.png
1247 Sat Feb 11 15:49:38 GMT 2012 game/black/knight.png
627 Sat Feb 11 15:49:44 GMT 2012 game/black/pawn.png
1668 Sat Feb 11 15:49:20 GMT 2012 game/black/queen.png
626 Sat Feb 11 15:49:26 GMT 2012 game/black/rook.png
1493 Sat Feb 11 15:48:30 GMT 2012 game/white/bishop.png
1889 Sat Feb 11 15:48:06 GMT 2012 game/white/king.png
1544 Sat Feb 11 15:48:40 GMT 2012 game/white/knight.png
1003 Sat Feb 11 15:48:48 GMT 2012 game/white/pawn.png
2440 Sat Feb 11 15:48:12 GMT 2012 game/white/queen.png
837 Sat Feb 11 15:48:20 GMT 2012 game/white/rook.png
The file where the error occurs is ChessSquare.class
img = new ImageIcon(getClass().getResource("/game/white/"+text+".png"));
Note the leading slash, as well as the game prefix.
As indicated in the comments, look in your jar (use winzip, 7zip, or something like that) to see where the resource is located. If it is not in a "white" directory then something is wrong with your jar build.
As to why it's running in Eclipse... that's likely because Eclipse is including the resource directory in the classpath when you run the program. This means that the getClass().getResource(...) call finds the png.
If you are keeping the file in a resources directory in Eclipse it also could be exporting the files into the META-INF directory in the jar, look in resources. If it is doing that then you may have setup the jar to be built for deployment into a web environment (see servlet 3.0 fragments).
One final note, be careful about using getClass().getResource(...) as it locks you down to only finding resources associated with the ClassLoader of the Class making that call (subject to how the ClassLoader hierarchy in question operates). You may want to provide extensions later where you can package other image sets in jars. In those cases you'll have to rely on slightly different mechanisms to get the images loaded (see ClassLoader.getResources as a starting point).
EDIT: This looks to be strictly an Eclipse problem. According to the way that the jar is structured the correct path is "game/white/king.png".
i'm an idiot, i finally figured out where the error was. As I wrote the output was "file:/E:/Desktop/Chess/bin/game/white/Pawn.png", notice the big P letter in Pawn.png. But the actual images' names started with small letters. That's why it didn't work. Still a mystery for me why then it worked in eclipse

Categories