java

What is the recommended way to log data that caused errors in JUnit?

I'm relatively new to JUnit, and I was writing a few of my first tests today. For some particular method, I wanted to pass random values (all of which are within the correct range). If the method fails for any reason, I want to know which value caused it to fail. So what's the recommended way of doing this? (Or is it bad to use random v...

Line-breaking widget layout for Android

I'm trying to create an activity that presents some data to the user. The data is such that it can be divided into 'words', each being a widget, and sequence of 'words' would form the data ('sentence'?), the ViewGroup widget containing the words. As space required for all 'words' in a 'sentence' would exceed the available horizontal spac...

Compare objects in LinkedList.contains()

I want to be able to have LinkedList.contains() return true for a custom comparator. Suppose that I have 1 LinkedList and 2 objects LinkedList<MyObject> myList = new LinkedList<MyObject>(); MyObject a = new MyObject("HELLO"); MyObject b = new MyObject("HELLO"); Technicaly, both objects are identical in terms of comparison (MyObject ...

Hibernate: Removing item from a List does not persist

I am having trouble when removing an item from a list. The list is defined in a superclass, but the Hibernate annotations are applied to property accessors in a subclass. There are two methods in the superclass that manipulate the list. The "add" method works fine, but the "remove" does not persist changes. I have checked my Cascade sett...

Tomcat Compression Does Not Add a Content-Encoding: gzip in the Header

I am using Tomcat to compress my HTML content like this: <Connector port="8080" maxHttpHeaderSize="8192" maxProcessors="150" maxThreads="150" minSpareThreads="25" maxSpareThreads="75" enableLookups="false" redirectPort="8443" acceptCount="150" connectionTimeout="20000" disableUploadTimeout="true" compression="on" compressionMinSize="128...

Export PDF pages to a series of images in Java

I need to export the pages of an arbitrary PDF document into a series of individual images in jpeg/png/etc format. I need to do this in in Java. Although I do know about iText, PDFBox and various other java pdf libraries, I am hoping for a pointer to some working example, or some how-to. Thanks. ...

How to get Maximum bytes from a port using a serversocketchannel

hi guys, I'm developing a multiple port reading application using nio package.Actually these ports recieve packets continously.Using nio package we have to reAD packets using channels.There should not be a packet loss.Here is my code.I've confusion in else if ((key.readyOps() & SelectionKey.OP_READ)== SelectionKey.OP_RE...

How to open a file with the default associated program

How do I open a file with the default associated program in Java? (for example a movie file) ...

Use Java to identify lines in a graph

Hi, I am working on an idea wherein I have to identify lines in a JPG or PNG file. The PNG file contains one graph of a single value - one x,y combination. For example, the graph looks like Y = mx+c. I would like to identify the line in the graph - if I can indentify the position of the pixel in the frame, I believe I can get back the...

Who/What implements Interfaces that I can use directly?

I came across several interfaces while learning JDBC - Connection, Statement, ResultSet etc... Does this imply that some classes somewhere, hidden from me, are implementing these interfaces, and providing their references when I need it? Is this because they need to be implemented differently depending on the Driver I'm using? ...

Does anyone disagree with the statement: "using switch is bad OOP style"?

I have seen it written in multiple threads/comments on stackoverflow that using switch is just bad OOP style. Personally I disagree with this. There will be many cases where you cannot add code (i.e. methods) to enum classes you want to switch on because you don't control them, perhaps they are in a 3rd-party jar file. There will be oth...

What's the point of using constants for property keys?

Lately, I've come across a lot of Java code that relies on "properties files" for configuration. But instead of plain old string literals, the code uses constants (static final Strings) to retrieve the property values . I find this extra level of indirection annoying because I need to perform TWO lookups in EITHER direction. If I star...

Do people provide multiple mechanisms for doing the same thing in an API?

Is it confusing to design an API with multiple ways of achieving the same outcome? For example, I have my own Date library (which is a simple wrapper around the Java Date/Calendar classes to distinguish a year-month-day, Date, from an instant-in-time, Instant and provide mechanisms to convert between the two). I started off with one meth...

How to break multiple foreach loop?

I have four foreach loops that iterate through the collections and based on a condition do something. Here is the code that I am writing now: boolean breakFlag = false; String valueFromObj2 = null; String valueFromObj4 = null; for(Object1 object1: objects){ for(Object2 object2: object1){ //I get some value from object2 valueFr...

How do I set the default Java installation/runtime (Windows)?

I'm in the situation where I've installed the JDK, but I can't run applets in browsers (I may not have installed the JRE). However, when I install the JRE, it clobbers my JDK as the default runtime. This breaks pretty much everything (eclipse, ant) - as they require a server JVM. There's no JAVA_HOME system property these days - it ju...

Error when I try to change the class name

class HelloObject { void speak() { System.out.println("Hello (from object)!"); } } class HelloTester { public static void main(String[] args) { HelloObject object = new HelloObject(); object.speak(); } } When I change the "HelloTester" class name to something like "HelloTester2", the program suddenly work...

Best practice for controlling access to a ".internal" package

I write Eclipse plugins and export some classes as an API while wanting to restrict access to other classes. I follow the common Eclipse practice of separating these classes into a ".internal" subpackage. However, then I can't use "package" or default level access on these classes since many of them need to be used by the classes I'm ...

How to supply cookie to Java HTMLDocument?

I'm trying to read a web site as an HTMLDocument; and the site requires either a cookie from a previous logon, or a response to a popup dialog. I'm thinking that supplying the necessary cookie is the easiest to accomplish, but I haven't found a way to do that. The code to open and read the document is: URL url = new URL(suppliedURL); ...

Lightweight java.io.InputStream implementation that supports mark() & reset()

Good day, Currently, we are using ByteArrayInputStream for our reset-able InputStream. My problem with it is that it consumes a lot of memory (it loads all the bytes it represents in memory unlike some other InputStream implementations). My question then is, is there any lighter implementation of InputStream which supports mark() & rea...

In Java, Prevent a field from being changed after being returned by instance method.

In a software development class at my university, the teacher kept mentioning that on a quiz we needed to make sure that a field returned by a getter needed to be "protected." I guess she meant that nothing outside the class should be able to change it. She didn't give much more of an explanation than that. For instance: class Foo { ...