Java FAQ (Wiki forum at Coderanch) (original) (raw)

Have any questions ? Ask at the Java In General forum. For more basic topics, check out the JavaBeginnersFaq.


Q: How can I run executables written in other languages from Java?

This involves the java.lang.Runtime and java.lang.Process classes.
All the details can be found in this article.

As of Java 5, there's a new class for handling this: JavaDoc:java.lang.ProcessBuilder


Q: How can I run scripts written in JavaScript|TCL|Python|Rexx|Groovy|ObjectScript|..., pass parameters to them and retrieve the results ?

The Bean Scripting Framework (BSF) library from Apache does this. It facilitates two-way integration between Java and a growing number of scripting languages.

Java's scripting API does something similar.


Q: What is the Observable class and and the Observer interface?

Some discussion and an example can be found here.

Note that these classes (and the approach they represent) are now deprecated. These days you would use an event bus like https://github.com/greenrobot/EventBus instead.


Q: What are Marker (or Tagging) Interfaces, and why don't they have any methods?

Marker interfaces are a mechanism of asserting a fact about a class, without adding any
functionality to it. As such, they represent metadata about that class. Some examples are:

From this list it's clear that marker interfaces have been used for widely differing purposes.

Marker interfaces are a misuse of interfaces, and should be avoided. Note that all the above example are rather old, and that no new ones have been added since. Ken Arnold, who was/is behind several Java APIs at Sun, sounds off on marker interfaces here, noting that they should rarely be used.

With the advent of annotations in Java 5 -which are a generic mechanism of adding metadata to a class-, marker interfaces have become obsolete, and no new ones should be defined.


Q: How can I evaluate an expression that a user enters, or create a Java class that includes it as a method?

There are a number of libraries that can take an expression and either compile or interpret it. JEP does interpretation; a newer commercial version is also available. For even more flexibility, check out Javassist, which creates actual Java classes.

This Javaranch Journal article demonstrates how to use Javassist to create classes that evaluate mathematical expressions.

Starting with Java 6, there's now an official API for working with the compiler from within Java code.


Q: How can I launch a web browser with a particular URL from Java code?

Starting with Java 6, the Desktop.browse(...) method can be used: JavaDoc:java.awt.Desktop

If the objective is to display a web page within a Swing application, the Lobo web browser component can be used. For very simple pages (HTML 3.2, no CSS, no JavaScript etc.) Swing contains a web browsing component. JavaFX contains a much improved web view component for HTML 5: JavaDoc:javafx.scene.web.WebView


Q: How can I implement a licensing scheme for my Java application?

Several commercial options are available; check out TrueLicense, license4j and JLicense.


Q: How can I call COM/DLL/ActiveX/.Net objects from Java?

This will involve using the JNI API (specification, introduction). A number of libraries exist that take some of the pain of using JNI out of it, like JACOB, Jawin and j-Interop, but they're all outdated. Several commercial tools (like EZ Jcom and JNBridge) are also available.

Since JNI only works with C/C++, but not the .Net languages, a .Net wrapper in C++ needs to be created as well if the COM/DLL object was written in one of those languages. An RFE (Request for Enhancement) has been filed years ago for letting JNI code access .Net code directly (see this entry in Sun's Java Bug Database), but it doesn't seem to go anywhere.

JNA implements something similar to JNI, but without the need to create or use C headers and files (it's all Java from the developer's perspective).


Q: What's up with integers not being equal to one another? The following code prints "true, true, false, true". Shouldn't it be "true, true, true, true"?

The key to understanding this is that the JVM uses a process called "boxing" (or "auto-boxing") when converting an int (like 127) to an Integer object. This involves calling the Integer.valueOf(127) method. The JavaDoc:java.lang.Integer#valueOf(int) says: "Returns a Integer instance representing the specified int value. If a new Integer instance is not required, this method should generally be used in preference to the constructor Integer(int), as this method is likely to yield significantly better space and time performance by caching frequently requested values." What that means is that the valueOf() method has a cache of Integer objects, and if the primitive being boxed is in that range, the cached object is returned. It just so happens that 127 is in that range, but 128 is not. So i and j are the same object, while i1 and j1 are not. (Also note that you can't rely on the boundary to fall between 127 and 128 - since this is not documented, JRE implementors are free to enlarge the range of values so cached.)


Q: How can I find out from where my class is loaded?


Q: Where can I download old JDK versions, and other APIs that have been deprecated?

Here: http://www.oracle.com/technetwork/java/archive-139210.html


CategoryFaq