Accessing a resource within a .jar

Posted by Emanuele Gesuato Sun, 05 Feb 2006 18:08:00 GMT

Sometimes it is useful to distribute an application in a jar file through Java Web Start or any other way. So, you could have to read some resource (images or properties file) from inside a jar. How can you do it ? It’s very simple, here’s an example to retrieve an image:

ImageIcon image = (new ImageIcon(getClass().getResource("yourpackage/mypackage/image.gif")));

In general, you can retrieve an InputStream in the following way:

InputStream is = this.getClass().getClassLoader()    .getResourceAsStream("yourpackage/mypackage/myfile.xml");

It will run inside or outside the jar. Enjoy !

Posted in  | Tags ,  | 478 comments | no trackbacks

Installing Java Studio Creator 2 on Debian

Posted by Dario Santamaria Thu, 26 Jan 2006 18:34:00 GMT

On Linux, the required distribution for Java Studio Creator 2 (now Free for all SDN members) is Fedora. I’m a Debian user and I succeeded to install it, however.
The first time my installation completes too soon, without correctly installing Java Application Server. I read the FAQs and I found a page saying:
Check if the following RPM packages exist on your system:

  • either libstdc++ or compat-libstdc++
  • either libstdc++-devel or compat-libstdc++-devel

If these are missing, first install them. Then restart the Java Studio Creator installer.

The correct Debian (Etch) packages (apt-get ‘em!) are

  • libstdc++2.10-glibc2.2 and
  • libstdc++2.10-dev

I don’t know if both are required or only the first one, but now the whole installation of Java Studio Creator 2 is complete.

Thanks to David Coldrick’s Weblog (installation on Ubuntu of JSCreator 1).

Posted in  | Tags , ,

Xom

Posted by Andrea Nasato Sun, 15 Jan 2006 15:22:00 GMT

Today I want to introduce you into XOM, which is one of the two arguments I’m going to talk about in our next meeting (the second is JSF 1.2). What is XOM? Someone could think it’s yet another XML library. In part that’s true.

Read more...

Posted in  | Tags ,  | 7 comments

Serialize a bean using jdbc

Posted by Enrico Giurin Tue, 10 Jan 2006 17:19:00 GMT

Sometimes, in my work as programmer, I need to serialize an object (bean) into a table, in a BLOB field, as well as retrieves bean from a ResultSet. I have realized a simple example that, using JDBC, allows to obtain this.

Read more...

Posted in  | Tags , ,  | 23 comments

Maven 2: Spring and JTA dependencies

Posted by Lucio Benfante Sat, 26 Nov 2005 08:06:00 GMT

Few weeks ago I started using Maven 2 for one of my development projects. The project is very simple, but it has dependencies with some external libraries, in particular with Spring:

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring</artifactId>
  <version>1.2.6</version>
  <scope>runtime</scope>
</dependency>
Read more...

Posted in  | Tags , , ,  | 80 comments

JSF and Facelets

Posted by Andrea Nasato Tue, 22 Nov 2005 10:42:00 GMT

One of the main issues with JSF is the plenty of use of custom tags in the view. If you don’t want to use a visual instrument like Java Studio Creator, building a page could become a nightmare, especially if your graphic designer uses a huge amount of html components.

If you ever tried Tapestry you know that with that framework this is not a problem, when you have the html template you have only to put the proper attribute to dynamically rendered tags and it’s all done.

Now also JSF allows you to do that: the project facelets aims to have the same approach used when developing with Tapestry. With facelets you can use templating and write a plain html file as a view.

Read more...

Posted in  | Tags ,  | 15 comments

JAXP 1.3 and DoS attacks

Posted by Andrea Nasato Sun, 13 Nov 2005 07:36:00 GMT

One of the new aspects introduced in JAXP 1.3 is the opportunity to specify the security level of the SaxParser used. The target of this feature is to prevent the application from DoS (Denial of Service) attacks, which use some vulnerabilities of XML. There are two main attack categories, let’s see them:

  1. Entity Resolution: suppose that XML stream requires a DTD and this DTD is in an external server and not local to your application. The parser requests the DTD to the server, which can send the DTD slowly or can give it malformed: in this case the parsing stops indefinitely. The solution to this problem is to set to a false value these properties:
    • parser.setFeature(   "http://xml.org/sax/features/external-general-entities",   false)
    • parser.setFeature(   "http://xml.org/sax/features/external-parameter-entities",   false)
    In this way the parser doesn’t call the external server to resolve the DTD.
  2. Overflow attack: XML doesn’t give a limit to the number of attributes of an element, and to the length of an element name. Overflow attacks start from this observation, and their objective is stop the server from fulfilling other requests: if the parser uses the DOM API it maintains a tree representation of XML in memory. When the parser encounters such XML streams it starts allocating objects for each attribute, saturating server memory. If you have such a problem and you use JAXP 1.3 you can set this property: http://javax.xml.XMLConstants/feature/secure-processing. With this property set, your parser rejects this kind of XML streams. You can have notification of such event in the fatalError method of the handler registered with the parser.

So if your boss is paranoid, or if your services could effectively be attacked in such a way, use JAXP 1.3 and those simple rules. If you want more information about JAXP 1.3, this is a good link. For a complete description of DoS attacks with XML this is a good tutorial.

Posted in  | Tags  | 2 comments

ToStringHelper

Posted by Lucio Benfante Fri, 11 Nov 2005 18:49:00 GMT

One of the traits I personally appreciate most in a good programmer is what I call the “constructive laziness”. It is the attitude to build software solutions for avoiding boring activities.

One of the most frequent and boring activities happening in the writing of a Java class is the implementation of the toString method, especially if the class contains lot of attributes and if it is frequently updated.

Last summer, in a mood of constructive laziness than surely doesn’t make me a good programmer, I wrote a small class library that tries to solve this problem. Using such library you can write a toString method as simple as the following:

public String toString() { ToStringHelper.toString(this); }

Read more...

Posted in  | 38 comments

Older posts: 1 2