The chances are all you really need are some name-value pairs to specify a few parameters. If this is the case Java comes with a wonderfully simple solution right out of the box, .properties files.
The syntax for a .properties file could not be simpler; all lines beginning with a # are comments and all other lines are in the form:
CODE:
1.
< key>= < value>
Below is a sample configuration for the Virtual Learning environment
CODE:
1.
# Basic portal config
2.
BASE_URL=www.eve.nuim.ie/evePortal
3.
4.
# Database stuff
5.
DB_JNDI_NAME=jdbc/eve_portal
6.
7.
# File locations
8.
DATA_DIR=/var/eve-data
9.
PDFLATEX_BIN=/opt/local/bin/pdflatex
10.
IMAGEMAGICK_BIN_DIR=/opt/local/bin
If you choose to use this type of configuration file you can locate and open it all in one step by using functionality built into the java class loader as follows:
JAVA:
1.
Properties configFile = new Properties();
2.
configFile.load(this.getClass().getClassLoader().getResourceAsStream("/my_config.properties"));
You can they read out any key in the following way:
JAVA:
1. some_var = configFile.getProperty("some_key");
Simple as that, no need to load complex parsing libraries, no big long messy code. Unless you have a good reason to go with a more complex format you really should be using .properties files.
Properties configFile = new Properties();
configFile.load(new FileInputStream(”configuration.conf”));
String foo = configFile.getProperty(”my_key”);
Wednesday, August 11, 2010
Monday, August 9, 2010
How to write a Java Application without a main method
You can write a runnable Java program which does not have main method at all. This can be done using the static block of the class.
The reason this works is that static initialization blocks get executed as soon as the class is loaded, even before the main method is called. During run time JVM will search for the main method after exiting from this block. If it does not find the main method, it throws an exception. To avoid the exception System.exit(0); statement is used which terminates the program at the end of the static block itself.
class MainMethodNot
{
static
{
System.out.println("This java program have run without the run method");
System.exit(0);
}
}
The reason this works is that static initialization blocks get executed as soon as the class is loaded, even before the main method is called. During run time JVM will search for the main method after exiting from this block. If it does not find the main method, it throws an exception. To avoid the exception System.exit(0); statement is used which terminates the program at the end of the static block itself.
class MainMethodNot
{
static
{
System.out.println("This java program have run without the run method");
System.exit(0);
}
}
Wednesday, August 4, 2010
Awt, Swing and SWT
AWT: The idea was to wrap the native GUI widgets of the various operating systems with a platform-independent Java API called Abstract Window Toolkit (AWT). Only common widgets such as text field, text area, check box, radio button, list, and push button were supported by AWT. The graphics and imaging features were also very limited. That was, at best, enough for building simple applets.
SWING: is one of the most complex GUI frameworks ever developed. It has a complete set of GUI components ranging from buttons and text fields to tables, trees, and styled text editors. These components do not rely on the native widgets of the operating system; instead, Swing components are painted using graphic primitives such as lines, rectangles, and text. The painting is delegated to a look and feel (L&F) plug-in that can imitate the native L&F. Swing also has a platform-independent L&F called "Metal." JBuilder, uses Swing, and its speed is quite good.
Standard Widget Toolkit (SWT) is the GUI toolkit developed by IBM for its Eclipse IDE. SWT can be used outside of the Eclipse environment and offers direct access to the native GUI features of the operating system. Therefore, SWT-based Java applications have native GUIs and can be integrated with other native applications and components. SWT delegates to native widgets for common components (such as labels, lists, tables, and so on) as AWT does, while emulating in Java more sophisticated components (for example, toolbars are emulated when running on Motif) similarly to Swing's strategy.SWT has been designed to be as inexpensive as possible. This means (among the other things) that it is native-oriented. Anyway, it differs from AWT in a number of details. SWT provides different Java implementations for each platform, and each of these implementations calls natively (through the Java Native Interface, JNI) the underlying native implementation. The old AWT is different in that all platform-dependent details are hidden in C (native) code and the Java implementation is the same for all the platforms.
SWING: is one of the most complex GUI frameworks ever developed. It has a complete set of GUI components ranging from buttons and text fields to tables, trees, and styled text editors. These components do not rely on the native widgets of the operating system; instead, Swing components are painted using graphic primitives such as lines, rectangles, and text. The painting is delegated to a look and feel (L&F) plug-in that can imitate the native L&F. Swing also has a platform-independent L&F called "Metal." JBuilder, uses Swing, and its speed is quite good.
Standard Widget Toolkit (SWT) is the GUI toolkit developed by IBM for its Eclipse IDE. SWT can be used outside of the Eclipse environment and offers direct access to the native GUI features of the operating system. Therefore, SWT-based Java applications have native GUIs and can be integrated with other native applications and components. SWT delegates to native widgets for common components (such as labels, lists, tables, and so on) as AWT does, while emulating in Java more sophisticated components (for example, toolbars are emulated when running on Motif) similarly to Swing's strategy.SWT has been designed to be as inexpensive as possible. This means (among the other things) that it is native-oriented. Anyway, it differs from AWT in a number of details. SWT provides different Java implementations for each platform, and each of these implementations calls natively (through the Java Native Interface, JNI) the underlying native implementation. The old AWT is different in that all platform-dependent details are hidden in C (native) code and the Java implementation is the same for all the platforms.
Tuesday, August 3, 2010
java.lang » System Properties
System Properties
* Getting and Setting the Value of a System Property
* Listing All System Properties
* Setting the Value of a System Property from the Command Line
Getting and Setting the Value of a System Property
// Get a system property
String dir = System.getProperty("user.dir");
// Set a system property
String previousValue = System.setProperty("application.property", "newValue");
--------------------------------------------------------------------------------
Listing All System Properties
// Get all system properties
Properties props = System.getProperties();
// Enumerate all system properties
Enumeration enum = props.propertyNames();
for (; enum.hasMoreElements(); )
{
// Get property name
String propName = (String)enum.nextElement();
// Get property value
String propValue = (String)props.get(propName);
}
---------------------------------------------------------------------------------
Setting the Value of a System Property from the Command Line
A system property can be set or overridden by specifying the -D option to the java command when running your program.
java -Dmy.prop="my value" MyApp
// Get the value of the system property
String prop = System.getProperty("my.prop");
// my value
* Getting and Setting the Value of a System Property
* Listing All System Properties
* Setting the Value of a System Property from the Command Line
Getting and Setting the Value of a System Property
// Get a system property
String dir = System.getProperty("user.dir");
// Set a system property
String previousValue = System.setProperty("application.property", "newValue");
--------------------------------------------------------------------------------
Listing All System Properties
// Get all system properties
Properties props = System.getProperties();
// Enumerate all system properties
Enumeration enum = props.propertyNames();
for (; enum.hasMoreElements(); )
{
// Get property name
String propName = (String)enum.nextElement();
// Get property value
String propValue = (String)props.get(propName);
}
---------------------------------------------------------------------------------
Setting the Value of a System Property from the Command Line
A system property can be set or overridden by specifying the -D option to the java command when running your program.
java -Dmy.prop="my value" MyApp
// Get the value of the system property
String prop = System.getProperty("my.prop");
// my value
OS' environment variables from within a Java program
import java.util.Map;
public class EnvMap {
public static void main (String[] args) {
Map env = System.getenv();
for (String envName : env.keySet()) {
System.out.format("%s=%s%n", envName, env.get(envName));
}
}
}
public class EnvMap {
public static void main (String[] args) {
Map
for (String envName : env.keySet()) {
System.out.format("%s=%s%n", envName, env.get(envName));
}
}
}
Monday, August 2, 2010
Subscribe to:
Posts (Atom)