Objective: SOS with Java silly-but-wasting-time coding errors
Contents
My code read two lines at a time?
Noope...sometimes you will get astonished about your System.in.read() in a small code that readlines from file, for example:
import java.io.*; class FileRead { public static void main(String args[]) { try{ FileInputStream fstream = new FileInputStream("textfile.txt"); DataInputStream in = new DataInputStream(fstream); BufferedReader br = new BufferedReader(new InputStreamReader(in)); String strLine; while ((strLine = br.readLine()) != null) { System.out.println (strLine); *** System.in.read();**** } in.close(); }catch (Exception e){//Catch exception if any System.err.println("Error: " + e.getMessage()); } } }
This code will show two lines at a time as It will get the trailing line feed from the previous System.out.println(strLine) and won't wait for your desired enter key press...silly huh
String comparison
Forget "==" or "!=" as they won't be reported as errors but won't work for comparing strings (its ascii content), the java way is
if (string1.equals(string2)) if (string1.compareTo(string2) < 0) if (string1.equalsIgnoreCase(string2))
Show icon embedded into jar file working in eclipse and in your executable file
For some reason I wasn't able to clearly identify, java or eclipse likes to complicate things when you want to show images that you want to pack in to your program jar file.
If you put the icon you want to display in your main frame in you "./images/" path into your eclipse working directory eclipse will say ok everything alright when you press the "Run" button. This would be the code:
myFrame.setIconImage(myFrame.getToolkit().getImage("images/no_more.PNG"));
Then when you decide to create your jar file (File/Export/Runnable Jar file) that you expect to run like in the Eclipse IDE... NOOOOOPE! the resulting file will NOT have the images directory embedded... this is something I hated as I wanted a unique portable jar with all files/resources packend inside.
You think about possible workarounds and you read about other functions like specifying paths like URLs?? ok lets try
URL imgURL = getClass().getResource("images/no_more.PNG"); myFrame.setIconImage(myFrame.getToolkit().getImage(imgURL));
}}} OUCH...Eclipse error the getClass wants a class/package or something that looks like it, so the dirty thing i dit was creating a jar file containing my desired PNG file inside a images folder and add it to the project in the menu "Project/Properties/Java Build Path/Libraries/Add Jars". Ok now eclipse does not complain.
The thing is in your exported jar file check that you PNG file is in the appropriate folder that match to the "getResource" function invocation you did in the code and it should work
If anyone has a hint into making this more straightforward please let me know!
Compiling, jaring, splash screen and MANIFEST.MF file
To compile just use
javac program.java
The MANIFEST.MF describes a jar itself and must for instance define the main class to execute. Here I present the manifest file from the well known webscarab http proxy
Manifest-Version: 1.0 Ant-Version: Apache Ant 1.6.5 Created-By: 1.5.0_06-b05 (Sun Microsystems Inc.) Main-Class: org.owasp.webscarab.WebScarab Built-By: rdawes Class-Path: lib/concurrent.jar lib/htmlparser.jar lib/bsh-2.0b1.jar li b/jfreechart-0.9.12.jar lib/jcommon-0.8.7.jar lib/bsf-2.3.0.jar lib/j hall-2.0_02.jar lib/chardet.jar lib/tagsoup-1.0rc2.jar lib/wsdl4j.jar lib/openamf.jar lib/commons-logging-1.0.4.jar Name: org/owasp/webscarab/ Implementation-Title: org.owasp.webscarab Implementation-Vendor: OWASP Foundation. Implementation-Version: 20070504-1631
In my case I wanted to add a nice/fast-to-include splash screen. In order to do so you don't need to code anything It can be done automatically if you specify so in the manifest file like this. (warning be very careful with file types, "PNG" != "png" for java so in the images file the same case sensitive file must exists)
Manifest-Version: 1.0 Class-Path: . Created-By: 1.6.0_14 (Sun Microsystems Inc.) SplashScreen-Image: images/no_more_splash.PNG Main-Class: NoMore
To jar some classes and add some images to the jar file once the manifest file is customize just use
jar cmf MANIFEST.MF output_file.jar program_classes.class images/*
For executing issue the following command
java -jar output_file.jar
No comments:
Post a Comment