java

Run external program from Java, read output, allow interruption

I want to launch a process from Java, read its output, and get its return code. But while it's executing, I want to be able to cancel it. I start out by launching the process: ProcessBuilder pb = new ProcessBuilder(args); pb.redirectErrorStream(true); Process proc = pb.start(); If I call proc.waitFor(), I can't do anything until the p...

Regular Expression : find a number near to a given String

I am trying to find a good way to capture a number that is at no more than N characters away from a given string. For example, if the String is "age" and N=4 have to find "Age 5" => 5 "My age is 10 and I my name is John" => 10 "My age is almost 5 and I my name is Mary" => null In the last case, the number is separated more than 4 cha...

How do I prevent JAXB from binding superclass methods of the @XmlRootElement when marshalling?

I have a class that is annotated as the @XmlRootElement with @XmlAccessorType(XmlAccessType.NONE). The problem that I am having is that the superclass's methods are being bound, when I do not want them to be bound, and cannot update the class. I am hoping there is an annotation that I can put on the root element class to prevent this f...

Synchronizing on an Integer value

Suppose I want to lock based on an integer id value. In this case, there's a function that pulls a value from a cache and does a fairly expensive retrieve/store into the cache if the value isn't there. The existing code isn't synchronized and could potentially trigger multiple retrieve/store operations: //psuedocode public Page getPa...

Is Java foreach iteration order over primitives precisely defined?

Example code: int a[] = new double[]{0, 1, 2, 3}; int result = 0; for (int i : a) result += i; Is the loop guaranteed to iterate across a[0], a[1], a[2], a[3] in that order? I strongly believe the answer is yes, but this page seems to not unambiguously state order. Got a solid reference? ...

Start program if not already running in Java

I need to start 1-3 external programs in my Java application that have paths defined by the user. I have few requirements: 1) I don't want the program to execute if it is already running 2) I don't want any of the programs to steal focus from my Java application 3) I don't care if any of them fail to start or not...they just need to ...

Call Java functions in Flex

Right now I'm trying to understand how Flex works with Java (Flex -> BlazeDS -> Java). I tried to follow THIS tutorial and everything works fine, I just don't understand why do we need to call java function this way: <mx:Script> <![CDATA[ import mx.rpc.events.FaultEvent; import mx.rpc.events.ResultEvent; //...

If else alternative

I have a program I am writing that works on the principle of populating a two dimensional array to check the winning condition, it's noughts and crosses so the two dimensional array is populated on the click of a button, 1 for a circle, 2 for a cross, then the checkWin() will work on this principle, not the actual code... if (myArray[0]...

What makes hot deployment a "hard problem"?

At work, we've been having a problem with "PermGen out of memory" exceptions, with the team lead deciding it was a bug in the JVM - something related to hot-deployment of code. Without explaining many details, he pointed out that hot deployment is a "hard problem", so hard that even .NET doesn't do it yet. I found a lot of articles expl...

Singleton in Java App Server.. How bad of an idea is this?

I am currently working on some older java code that was developed without App Servers in mind. It is basically a bunch of "black box code" with an input interface, and an output interface. Everything in the "black box" classes are static Data Structures that contain state, which are put through algorithms at timed intervals (every 10 s...

Change the alpha value of a BufferedImage?

How do I change the global alpha value of a BufferedImage in Java? (I.E. make every pixel in the image that has a alpha value of 100 have a alpha value of 80) ...

SQL parser library for Java

Is there a good open-source Java library for parsing SQL statements? If possible, it should be customizable or flexible enough to also be able to parse (or at least ignore) vendor-specific syntax (such as Oracle tablespace definitions or MySQL's LIMIT clause). If not, strict adherence to the SQL standard is also fine. Update: I need t...

Binding to a socket in Tomcat

I am writing a Tomcat app. As part of its functionality, it needs to send out a UDP multicast when certain events happen. Right now my code goes something like this (host and group are fake, exception handling ripped out to save space): import java.net.*; /* ..... */ DatagramSocket socket = new DatagramSocket(12345); InetAddress group...

JSTL <c:out> where the element name contains a space character...

I have an array of values being made available, but unfortunately some of the variable names include a space. I cannot work out how to simply output these in the page. I know I'm not explaining this well (I'm the JSP designer, not the Java coder) so hopefully this example will illustrate what I'm trying to do: <c:out value="${x}"/> ...

Is there a tutorial on how to use JEditTextArea in my application?

I'm trying to embed the JEditTextArea into my application, which is available here: http://syntax.jedit.org/ I was wondering if there was a definitive guide to doing this? I am having problems getting the packages to work, and am not sure how I'm supposed to even go about importing them into my project. If anyone has gotten this to wo...

How do you add a directory of java classes to a project?

I have a Java project I'm working on, and wish to include a directory full of classes. These are the "JEdit Syntax" classes, and come within two packages: org.syntax.jedit org.syntax.jedit.tokenmarker However, everywhere I look it tells me to "import the entire jar file". My problem is that there is no jar file, just a directory wit...

Threading in Java: How to lock an object?

Hi, The following Function is executing in its own thread: private void doSendData() { try { //writeToFile(); // just a temporary location of a call InetAddress serverAddr = InetAddress.getByName(serverAddress); serverAddr.wait(60000); //Log.d("TCP", "C: Connecting..."); Socket socket ...

How can I cause the browser to navigate to a given URL from a Java applet?

I have a Java applet consisting of a wizard-like form with three steps. At the final step, the user can click a "finish" button. I want that button to cause the web browser to navigate to a specific URL. Question: Is there a way to cause the browser to navigate to a specific URL, in such a way that it will work across all modern browser...

Windows development for hardcore Java guy

I'm a little ashamed for asking this, because, you know, I've been very anti-MS for a long time. I have been fortunate enough as to make a living outside Visual Studio. As I grown older there are "some" things I would like to try, and to be very honest, this Q&A site has inspired me and I realize that VS and Windows Development doesn't ...

How do I include .class files in my project in Eclipse? (Java)

Hey all. I am working on a project for school where we are given the .class file but not the source to include in our code. I am using Eclipse, and I want to include the file in my project so I can instantiate objects from it and use it. The file is TokenizerImpl.class, and I want to use it like this: TokenizerImpl tokenizer = new Token...