java

JavaSound writing to audio file with a stream

I'm attempting to create an audio filter using the JavaSound API to read and write the audio files. Currently my code is structured as follows: ByteArrayOutputStream b_out = new ByteArrayOutputStream(); // Read a frame from the file. while (audioInputStream.read(audioBytes) != -1) { //Do stuff here.... b_out.writ...

programmatically checking for open connection in JDBC

How do I check for an open connection in jdbc for oracle database? Note: conn.isClosed() cannot be used for this. ...

How can I call Perl from Java?

I have a Perl module that I would like to use from Java. Is there a way to call this code using either ActiveState Perl on Windows or the generic Perl that comes with Linux? I have found references to JPL but it doesn’t appear to be maintained anymore. ...

What is the best way to call C/C++ code from other languages such as Java, PHP, Perl, Python, etc?

What is the best way to call C/C++ from other languages such as Java, Python, Perl, PHP, etc? ...

Java - Setting context attributes (ServletContextListener)

I read a properties-file at the webapplication startup phase (contextInitialized()) and I started to think about how to make these settings 'visible' to the servlets. Do I need to loop through the keys and add each and every one to the context, like this Iterator i = settings.keySet().iterator(); while (i.hasNext()) { key = (String) i....

Java: How do I know which jar file to use given a class name?

Given a class, org.eclipse.ui.views.navigator.ResourceNavigator for example, how do I find out which jar file to use? I know it's in org.eclipse.ui.ide, but how would I find that out? Edit: Thank you to all of you who answered. Like many things, there seems to be several ways to skin this cat. I wish javadoc contained this info. So far ...

Java: Print a 2D String array as a right-justified table

What is the best way to print the cells of a String[][] array as a right-justified table? For example, the input { { "x", "xxx" }, { "yyy", "y" }, { "zz", "zz" } } should yield the output x xxx yyy y zz zz This seems like something that one should be able to accomplish using java.util.Formatter, but it doesn't seem to allow n...

Proper coupling for multiple listeners that need to accessed shared state data

Working with a traditional listener callback model. I have several listeners that collect various stuff. Each listener's collected stuff is inside the listener in internal structures. The problem is that I want some of the listeners to be aware of some of the "stuff" in the other listeners. I enforce listener registration order, so if ...

java ee | ejb3 | runtime dispatch

Hi, I would like to call an ejb3 at runtime. The name of the ejb and the method name will only be available at runtime, so I cannot include any remote interfaces at compile time. String bean = 'some/Bean'; String meth = 'doStuff'; //lookup the bean Object remoteInterface = (Object) new InitialContext().lookup(bean); //search the meth...

Add leading zeroes in Java

Is there a better way of getting this result? This function fails if num has more digits than digits, and I feel like it should be in the library somewhere (like Integer.toString(x,"%3d") or something) static String intToString(int num, int digits) { StringBuffer s = new StringBuffer(digits); int zeroes = digits - (int) (Math.lo...

Is there a way to split strings with String.split() and include the delimiters?

I'm trying to split a string with all non-alphanumeric characters as delimiters yet Java's String.split() method discards the delimiter characters from the resulting array. Is there a way to split a string like the "\W" regex pattern does, yet keep the delimiters? ...

How can I draw something on a jPanel that will not be repainted ?

How can I draw something in JPanel that will stay the same and not be repainted, I am doing a traffic simulation program and I want the road to be drawn once because It will not change. Thanks ...

Modifying graphics context in Java

I have a form that tries to modify a JComponent's graphics context. I use, for example, ((Graphics2D) target.getGraphics()).setStroke(new BasicStroke(5)); Now, immediately after I set the value and close the form, the change is not visible. Am I not allowed to modify a JComponent's graphics context? How else would I modify the stroke,...

How do I count the number of occurrences of a char in a String?

I have the string a.b.c.d I want to count the occurrences of '.' in an idiomatic way, preferably a one-liner. (Previously I had expressed this constraint as "without a loop", in case you're wondering why everyone's trying to answer without using a loop). ...

How to change data in JTable cells?

I can set data in JTable constructor, and then user can change this data when program is running manually(typing from keyboard). But what method should I use in case I want to change data in some column? To change column header I use TableColumn method setHeaderValue. What should I use to set value in JTable cell? ...

How to disable (or hide) the close (x) button on a JFrame?

I have a window (derived from JFrame) and I want to disable the close button during certain operations which are not interruptible. I know I can make the button not do anything (or call a handler in a WindowListener) by calling setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); but I would like to make it clear visually that it is...

Feasibility of LinkedList vs. Array for sorted vs. unsorted data?

Comparing LinkedLists and Arrays while also comparing their differences with sorted and unsorted data Adding Removing Retrieving Sorting Overall speed Overall memory usage Actual questions Discuss the feasibility of implementing an unsorted data set as a linked list rather than an array. What would the tradeoffs be in terms of...

Capturing image from webcam in java?

How can I continuously capture images from a webcam? I want to experiment with object recognition (by maybe using java media framework). I was thinking of creating two threads one thread: Node 1: capture live image Node 2: save image as "1.jpg" Node 3: wait 5 seconds Node 4: repeat... other thread: Node 1: wait until image is c...

Coding using sequence diagrams

When I was at university a lecturer said how some people managed to code using sequence diagrams, this to me seems a good way of programming but of course the devil is in the details. I have a few questions. Does this actually happen or was I misinterpreting what he said? Has anyone here actually done this? Is it more productive? Wh...

Cross-class-capable extendable static analysis tool for java?

I'm trying to write rules for detecting some errors in annotated multi-threaded java programs. As a toy example, I'd like to detect if any method annotated with @ThreadSafe calls a method without such an annotation, without synchronization. I'm looking for a tool that would allow me to write such a test. I've looked at source analyze...