java

What are the differences between Generics in C# and Java... and Templates in C++?

I mostly use Java and generics are relatively new. I keep reading that Java made the wrong decision or that .NET has better implementations etc. etc. So, what are the main differences between C++, C#, Java in generics? Pros/cons of each? ...

Java Logging vs Log4J

Hi there, is it still worth to add the log4j library to a Java 5 project just to log let's say some exceptions to a file with some nice rollover settings. Or will the standard util.logging facility do the job as well? What do you think? Thanks for sharing your thoughts! Okami ...

What are the best practices for JSF?

I have done Java and JSP programming in the past, but I am new to Java Server Faces and want to know if there's a set of best practices for JSF development. ...

Resettable Java Timer

I'd like to have a java.utils.Timer with a resettable time in java.I need to set a once off event to occur in X seconds. If nothing happens in between the time the timer was created and X seconds, then the event occurs as normal. If, however, before X seconds has elapsed, I decide that the event should occur after Y seconds instead, th...

How to remove debug statements from production code in Java

Is it possible for the compiler to remove statements used for debugging purposes (such as logging) from production code? The debug statements would need to be marked somehow, maybe using annotations. It's easy to set a property (debug = true) and check it at each debug statement, but this can reduce performance. It would be nice if the ...

Practical programming test in interview

I have been invited to do a second interview for a company recruiting for a software engineer. This interview will consist of a 45 minute programmatic test on a laptop followed by a whiteboard presentation on the solution. This position is Java/J2EE based so I'm assuming (hoping) the test will be implemented using Java. Have you ever do...

any site with a good resume of Java related APIs and technologies?

Hello today in the java Sun page there is an article about JAXB technology wich helps on create XML schemas and create java objects instances from/to xml documents. This tech could help me if have known it before. The same happens to me with JPA. There are a lot of Java technologies and APIs and is difficult known all them and know for...

Passing null to a method

I am in the middle of reading the excellent Clean Code One discussion is regarding passing nulls into a method. public class MetricsCalculator { public double xProjection(Point p1, Point p2) { return (p2.x - p1.x) * 1.5; } } ... calculator.xProjection(null, new Point(12,13)); It represents different ways of handling t...

List in JScrollPane painting outside the viewport

I have a list, each item of which has several things in it, including a JProgressBar which can be updating a lot. Each time one of the items updates its JProgressBar, the ListDataListener on the list tries to scroll it the visible range using /* * This makes the updating content item automatically scroll * into view if i...

How do I restrict JFileChooser to a directory?

I want to limit my users to a directory and its sub directories but the "Parent Directory" button allows them to browse to an arbitraty directory. How should I go about doing that? ...

How to discover a File's creation time with Java?

Is there an easy way to discover a File's creation time with Java? The File class only has a method to get the "last modified" time. According to some resources I found on Google, the File class doesn't provide a getCreationTime() method because not all file systems support the idea of a creation time. The only working solution I foun...

Alternatives to System.exit(1)

For various reasons calling System.exit is frowned upon when writing Java Applications, so how can I notify the calling process that not everything is going according to plan? Edit: The 1 is a standin for any non-zero exit code. ...

Do Java multi-line comments account for strings?

This question would probably apply equally as well to other languages with C-like multi-line comments. Here's the problem I'm encountering. I'm working with Java code in Eclipse, and I wanted to comment out a block of code. However, there is a string that contains the character sequence "*/", and Eclipse thinks that the comment should en...

How do I load an org.w3c.dom.Document from XML in a string?

I have a complete XML document in a string and would like a Document object. Google turns up all sorts of garbage. What is the simplest solution? (In Java 1.5) Solution Thanks to Matt McMinn, I have settled on this implementation. It has the right level of input flexibility and exception granularity for me. (It's good to know if the err...

How do you configure HttpOnly cookies in tomcat / java webapps?

After reading Jeff's blog post on Protecting Your Cookies: HttpOnly. I'd like to implement HttpOnly cookies in my web application. How do you tell tomcat to use http only cookies for sessions? ...

Using Hibernate to work with Text Files

Hey all, I am using Hibernate in a Java application to access my Database and it works pretty well with MS-SQL and MySQL. But some of the data I have to show on some forms has to come from Text files, and by Text files I mean Human-Readable files, they can be CSV, Tab-Delimited, or even a key, value pair, per line since my data is as si...

JSF Lifecycle and Custom components

There are a couple of things that I am having a difficult time understanding with regards to developing custom components in JSF. For the purposes of these questions, you can assume that all of the custom controls are using valuebindings/expressions (not literal bindings). Although I'll gladly upvote those who give good explanations on...

How do I recover from an unchecked exception?

Unchecked exceptions are alright if you want to handle every failure the same way, for example by logging it and skipping to the next request, displaying a message to the user and handling the next event, etc. If this is my use case, all I have to do is catch some general exception type at a high level in my system, and handle everything...

My (Java/Swing) MouseListener isn't listening, help me figure out why.

So I've got a JPanel implementing MouseListener and MouseMotionListener: import javax.swing.*; import java.awt.*; import java.awt.event.*; public class DisplayArea extends JPanel implements MouseListener, MouseMotionListener { public DisplayArea(Rectangle bounds, Display display) { setLayout(null); setBounds(bounds...

How do I add a MIME type to .htaccess?

I would like to add the following MIME type to a site run by Apache: <mime-mapping> <extension>jnlp</extension> <mime-type>application/x-java-jnlp-file</mime-type> </mime-mapping> (That is the Tomcat format.) I'm on a shared host, so I can only create an .htaccess file. Would someone please specify the complete contents of such a...