Feeds:
Posts
Comments

Oracle today announced the Oracle Exalogic Elastic Cloud T3-1B, a new model that brings the industry-leading performance, scalability and availability of SPARC Solaris servers to Oracle Exalogic Elastic Cloud engineered systems.
Oracle Exalogic Elastic Cloud is the world’s first and only integrated cloud machine—hardware and software engineered together to provide a “cloud in a box”. Exalogic is designed to revolutionize data center consolidation, enabling enterprises to bring together tens, hundreds, or even thousands of disparate, mission-critical, performance-sensitive workloads with maximum reliability, availability, and security. Oracle Exalogic’s unique high-bandwidth, low-latency interconnect fabric means that complex, distributed applications can run with a responsiveness simply not achievable with typical servers used in data centers today.

Oracle Exalogic’s extreme performance, massive scale, and hardware-based application isolation make it the ideal platform for consolidating many existing applications on a single platform. Applications can be migrated unchanged and then run with higher performance and reliability at a cost that can be as much as 60 percent lower than for traditional environments.

Exalogic’s management automation coupled with the dynamic scalability of Oracle WebLogic Server and Oracle Coherence, make it the ideal foundation for elastic cloud infrastructure.

“Exalogic networks together a lot of small machines with InfiniBand so it really looks like one large computer” said Ellison. “The secret sauce is memory management software called Oracle Coherence, which lets you scale it from a quarter rack up to eight racks as a cloud. That’s one big honking’ cloud.”

The extreme performance that Oracle Exalogic Elastic Cloud offers, especially for Java-based applications running on Oracle WebLogic Server and other Oracle Fusion Middleware technologies. For example, internal testing revealed that one rack of Oracle Exalogic Elastic Cloud demonstrated a 12-fold improvement for internet applications, to more than 1 million HTTP requests per second. Java messaging applications showed a 45-fold improvement, to more than 1.8 million messages per second.

 

Sources :

http://www.oracle.com/us/corporate/press/191712

I have tried learning about the forward and reverse proxies for sometime but got a perfect explanation from Sun’s[oracle] article .. 

To keep it straight — A forward proxy is the one which stands between a client and number of other servers , example : outgoing Internet requests from corporate employees .

                                           A reverse proxy is just exaclty the opposite, It stands before a server and number of  clients.

Example : incoming requests for a corporate Web site

I am trying to setup a reverse proxy in Iplanet at the moment and would be posting the same shortly. If it might help .

http://www.oracle.com/technology/products/weblogic/howto/zero-data-loss/wls_zerodataloss_howto.html.

I came across assigning urls to Tomcat from : http://tomcat.apache.org/tomcat-3.3-doc/mod_jk-howto.html

In an instance when you will have to redirect a particular request based on context from apache to the respective application server say Tomcat,[ where Mod_JK] is used. we will have to add the details as below.

Assigning URLs to Tomcat

If you have created a custom or local version of mod_jk.conf as noted above, you can change settings such as the workers or URL prefix.Use mod_jk’s JkMount directive to assign specific URLs to Tomcat. In general the structure of a JkMount directive is:

JkMount <URL prefix> <Worker name>

For example the following directives will send all requests ending in .jsp or beginning with /servlet to the “ajp13” worker, but jsp requests to files located in /otherworker will go to “remoteworker“.

JkMount /*.jsp ajp13
JkMount /servlet/* ajp13
JkMount /otherworker/*.jsp remoteworker

You can use the JkMount directive at the top level or inside <VirtualHost> sections of your httpd.conf file.

Qouted from :

http://raymondtay.blogspot.com/2008/09/j2eeweblogic-performance-tuning-with.html.

One of the excellent posts I have ever read.

his article contains a series of investigations for a customer of mine where the environment is running a WebLogic cluster of 20 machines in round-robin on HP-UX to service a global J2EE application and it performed slowly during peak periods and occasional hangs. The application was a typical 3-tier architecture whereby web relegates requests to the middle-tier (EJBs, MQs, MDBs) and this middleware goes to the Database-tier (SQL inserts, updates, deletes, stored procedures etc). The application was found to be experiencing heavy load during peak periods everyday.

There were a couple of issues related to poor performing SQLs, poorly designed middleware apps, WebLogic cluster design and runtime issues, JVM memory consumption and frequent garbage collections. Let me try to detail them a bit without giving away too much customer information. Hopefully, it can help you in your investigations in your environment.

During the peak period, the major contributing factors of the apps slow-ness were:

The heap size was 1.5GB (min,max), 512MB for Eden and the PermGen was 192MB. The minor GC kicked in frequently releasing approximately 60MB on average; the major GC kicked in twice every minute (avg. 3-5s on average, 40s on max) releasing 400 – 500 MB each time and reverse engineering the figures reveals that the object creation rate was roughly 800 M – 1.0 GB per minute. As GC is primarily a CPU-intensive operation (with saving state, freeing memory, compacting the heap etc). The large object creation rate combined with the relatively long pauses GCs occurences suggests that the application are creating objects in an in-efficient manner and that created problems with the cluster’s session replication mechanism as the users of the system would see stale data – due to long pauses in GC, the data in the session was not replicated *properly* across to the other servers.

Applications were attached to the WebLogic system classpath which meant that the Java classes were never unloaded from memory and combined with the fact that there are ALWAYS classloader leaks meant that whenever the operation team redeploy a.k.a “hot”-redeployment the apps, it worsens the memory footprint since the previous memory was never release due to this leakage. If you keep hot-deploying these stuff you will almost certainly get an OutOfMemory Error: PermGen out of space.

EJBs (4,000+ EJBs deployed, in my opinion too many) were utilizing remote interfaces when there was no need as those apps were not doing cross-vm operations and based on my previous experimentation, you would get a 3-fold runtime improvement when you convert the EJBs to local interfaces. This improvement is because there is less object marshalling/unmarshalling via RMI since everything is on the same JVM heap and consumes less system resources like file descriptors/socket & memory since local interfaces implies a local/normal Java call.

As i mentioned previously, the apps were deployed in the cluster and that meant that all persistent objects (e.g. session data, user preferences etc) must be Serializable (i.e. persistent objects need to implement java.lang.Serializable) since there would be session replication across the servers in the cluster which further degraded the performance as the cluster needs to maintain state across all 20 machines. Source code analysis found that user’s were keeping results of database fetches in session data! You can imagine the pressure faced by the JVM memory subsystem + WebLogic cluster replication.

WebLogic cluster was also malfunctioning during peak periods throwing an exception message like <WorkManager> <BEA-002911> <WorkManager weblogic.kernel.System failed to schedule a request due to weblogic.utils.UnsyncCircularQueue$FullQueueException: Queue exceed maximum capacity of: ’65536′ elements and this is an critical error thrown from the Work Manager which replaced the BEA traditional thread pool. What this meant was that the WebLogic cluster could no longer handle user’s requests and hanged. *I plan to unravel this mystery in a while to understand why this is happening*

The hardware loadbalancer was in “sticky” mode even though the WebLogic cluster was in round-robin mode which negated this round-robin-ness and resulted in certain servers encountering more stress than others and this was made worse by the long session timeout of 20+ hrs. That’s the cost of doing business….

After tracing the SQL statements execution times, it was found that they were causing alot of problems from missing indexes, lack of functional indexes, improper SQL statements which causes large database table joins and many “select count(*)…” from large table joins statements contributed to this object creation rate.

When i looked at these issues, the first couple of items i advised my customer was to do the following:
(1) Convert the EJBs to use local interfaces i.e. call-by-reference
(2) Tune the SQL statements via SQL reordering, indexes etc
(3) Tune the JVM heap to use more aggressive + parallel heap collectors via -XX:+UseParallelGC -XX:+UseConcMarkSweepGC (We are still experimenting this portion)
(4) Do not use system classpath to load application classes
(5) Review source codes to remove known classloader leaks

 

Part 2:

In my previous article, my tuning project with a customer ran into some trouble with WebLogic’s Work Manager and in particular, on the Java exception weblogic.utils.UnsyncCircularQueue$FullQueueException where the WebLogic server indicated that the queue where the server works on submitted requests. Checked the WebLogic docs and accordingly, the server will automatically resize the queue to fit the requests but what the docs didn’t mention was that the resize will fail to work if the size of the queue equals the capacity which happens to be 65536 and that’s the reason why it threw the error message “Queue exceeded the maximum capacity of ’65536′ elements”.

However, checking the code reveals something quite peculiar and that is the constructor suggests that only queues of sizes exceeding 1 GB will throw this error but the default capacity is always 256 and reaching a maximum of 65536 elements and btw, its WebLogic 9.2 ; so my guess is that source codes need to be cleaned up ? If anybody has any idea why, do drop me a comment, thanks in advanced.

 

Part 3:

 

 

jmap -histo <pid>   num     #instances         #bytes  class name
———————————————-
   1:         41202       27502288  [C
   2:          2304        1332520  [I
   3:         37934         910416  java.lang.String
   4:          5130         503632  <constMethodKlass>
   5:          5130         412968  <methodKlass>
   6:         12348         395136  fr.xebia.memory.bean.Person
   7:         12846         308304  java.util.concurrent.ConcurrentHashMap$HashEntry
   8:          7650         285656  <symbolKlass>
   9:         12518         200288  java.lang.Long
  10:           349         189184  <constantPoolKlass>
  11:           349         138704  <instanceKlassKlass>
  12:           327         132144  <constantPoolCacheKlass>
  13:            38         115456  [Ljava.util.concurrent.ConcurrentHashMap$HashEntry;
  14:           414          76392  [B
  15:           422          40512  java.lang.Class
  16:           556          32656  [[I
  17:           518          32056  [S
  18:            44          14080  <objArrayKlassKlass>
  19:           315          13480  [Ljava.lang.Object;
  20:           576           9216  java.lang.StringBuffer
…..
 184:             1              8  java.lang.System$2
 185:             1              8  java.io.File$1
 186:             1              8  java.lang.Terminator$1
Total        142211       32693376

 

First places are often trustees by the basic objects (java.lang.String, java.lang.Object, java.lang.Class) and declination in collections (java.util .* and tables, symbolized by [ ‘). If you find classes in your application in the top 20, they become suspicious. In our example, the histogram shows that the class’ fr.xebia.memory.bean.Person is instantiated 12,348 times for a memory footprint of 308,304 bytes. It is closely followed by entries from a ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap $ HashEntry) Obtaining this histogram is very fast and somewhat disrupts the FMV (Full GC to close;)). It is possible to do regularly (eg every ‘n’ seconds) and thus deduce trends: number of instances of a class increases, size of instances of a class that decreases .. . Note: with JVM version 1.4.2, it is also possible to obtain a histogram for each thread dump by adding the option-XX: + PrintClassHistogram start of the JVM. As histo-jstat, this option automatically implies a Full GC memory of the JVM. The histogram lets you know the classes that occupy the most memory, it does not indicate who was allocated. The dump The histogram is good, but not sufficient: it is sometimes necessary to obtain a complete dump of the memory to determine accurately the chain of referral. Jmap The tool also allows a complete dump and binary memory of a JVM. Two important notes: during the dump, nothing running in the JVM, all threads are stopped, whether applications or systems. the duration of the dump is proportional to the size of the memory used and depending on the size of the remaining memory (it takes a bit of free memory to run the treatment of the dump!). Example: the duration of a dump of a JVM with a heap of 1 Go to the saturation limit of memory is counted in minutes! Example: Creating a dump

Node Managers Weblogic Server utility to start, stop and restartAdministration and Managed Server Instances from remote location (There are other ways as well to start/stop Weblogic check here – Node Manager is optional component).

1. Node Manager Process is associated with a Machine and NOT with specific Weblogic Domain (i.e. Use one node manager for multiple domains on same machine)

2. There are two versions of Node Manager – Java-based and Script-based

Java-based node manager – runs with in JVM (Java Virtual Machine) Process and more secure than script-based node manager. Configuration for java-based node manager are stored in nodemanager.properties

Script-based node manager – is available for Linux and Unix systems only and is based on shell script.

3. There are multiple ways to access Node Manager
- From Administration Console : Environments -> Machines -> Configuration -> Node Manager
- JMX utilities (Java Management eXtension) more here
- WLST commands (WebLogic Scripting Tool)

4.Default port on which node manager listen for requests is localhost:5556, When you configure Node Manager to accept commands from remote systems, you must uninstall the default Node Manager service, then reinstall it to listen on a non-localhost (IP’s other than 127.0.0.1) listen address.

5. Any domain created before creation of Node Manager Service will not be accessible via node Manager(even after restarting node manager), solution is to run the WLST command “nmEnroll” to enroll that domain with the Node Manager.

6. Any domains created after the Node Manager service has been installed should not have to be enrolled against the Node Manager. The Node Manager should automatically be ‘reachable‘ by the domain.

.
How to Configure Node Manager ?

1.Configure each computer (on which you wish to use Node Manager) as a Machine in WebLogic Server
Environments -> Machines -> New (Add Machine) Use Link here
Environments -> Machines -> Machine Name (created above) -> Configuration -> Node Manager

2. Assign each server instance (Admin or Managed that you wish to control with Node Manager) to Machine.
Environments -> Machines -> Machine Name (created above) -> Configuration -> Servers -> Add (Add Server running on this node which you would like to monitor using Node Manager) for more info Click here

3. Enroll domain (created before installation of Node Manager) to Node Manager

Windows
cd $BEA_HOME\user_projects\domains\\bin
setDomainEnv.cmd
java weblogic.WLST
wls> connect(’weblogic’,’weblogic’, ‘t3://mymachine.mydomain:7001′)
wls> nmEnroll(’C:\bea\user_projects\domains/’, ‘C:\bea\wlserver_/common/nodemanager’)

.
Unix /Linux
cd $BEA_HOME/user_projects/domains//bin/
. setDomainEnv.sh
java weblogic.WLST
wls> connect(’weblogic’,’weblogic’, ‘t3://mymachine.mydomain:7001′)
wls> nmEnroll(’$BEA_HOME/user_projects/domains/’, ‘$BEA_HOME/wlserver_/common/nodemanager’)

where “mymachine.mydomain:7001″ is the reference to the Admin Server of the domain to which the server and machine definition belongs

.
How to start Node Manager ?

$WL_HOME\server\bin\startNodeManager.sh (startNodeManager.cmd on Windows)

How to install Node Manager as Service on Windows ?

Use $WLS_HOME\server\bin\installNodeMgrSvc.cmd (Where default WLS_HOME location is c:\bea\wlserver_)

To uninstall Node Manager Service on windows use $WLS_HOME\server\bin\uninstallNodeMgrSvc.cmd

installNodeMgrSvc.cmd will create Windows server with name as Oracle WebLogic NodeManager (C_bea_wlserver_)
.
Important Configuration files

– $WL_HOME/common/nodemanager/ nodemanager.properties, nodemanager.domains, nm_data.properties

–$DOMAIN_HOME/config/nodemanager/nm_password.properties

–$DOMAIN_HOME/servers//data/nodemanager/ boot.properties, startup.properties, server_name.addr, server_name.lck, server_name.pid, server_name.state

check-passthrough

The check-passthrough ObjectType SAF checks to see if the requested resource (for example, the HTML document or GIF image) is available on the local server. If the requested resource does not exist locally, check-passthrough sets the type to indicate that the request should be passed to another server for processing by service-passthrough.

The check-passthrough SAF accepts the following parameters:

*

type – (Optional) The type to use for files that do not exist locally. If not specified, type defaults to magnus-internal/passthrough.

What’s a Session?

SSL makes a distinction between a connection and a session. A connection represents one specific communications channel (typically mapped to a TCP connection), along with its keys, cipher choices, sequence number state, etc. A session is a virtual construct representing the negotiated algorithms and the master_secret . A new session is created every time a given client and server go through a full key exchange and establish a new master_secret.

Multiple connections can be associated with a given session. Although all connections in a given session share the same master_secret, each has its own encryption keys. This is absolutely necessary for security reasons because reuse of bulk keying material can be extremely dangerous. Resumption allows the generation of a new set of bulk keys and IVs from a common master_secret because the keys depend on the random values, which are fresh for each connection. The new random values are combined with the old master_secret to produce new keys.
How It Works

The first time a client and server interact, they create both a new connection and a new session. If the server is prepared to resume the session, it assigns the session a session_id and transmits the session_id to the client during the handshake. The server caches the master_secret for later reference. When the client initiates a new connection with the server, it provides the session_id to the server. The server can choose to either resume the session or force a full handshake. If the server chooses to resume the session, the rest of the handshake is skipped, and the stored master_secret is used to generate all the cryptographic keys.

As part of the Java language, the java.lang package is implicitly imported into every Java program. This package’s pitfalls surface often, affecting most programmers. This month, I’ll discuss the traps lurking in the Runtime.exec() method.

Pitfall 4: When Runtime.exec() won’t
The class java.lang.Runtime features a static method called getRuntime(), which retrieves the current Java Runtime Environment. That is the only way to obtain a reference to the Runtime object. With that reference, you can run external programs by invoking the Runtime class’s exec() method. Developers often call this method to launch a browser for displaying a help page in HTML.

There are four overloaded versions of the exec() command:

public Process exec(String command);
public Process exec(String [] cmdArray);
public Process exec(String command, String [] envp);
public Process exec(String [] cmdArray, String [] envp);

For each of these methods, a command — and possibly a set of arguments — is passed to an operating-system-specific function call. This subsequently creates an operating-system-specific process (a running program) with a reference to a Process class returned to the Java VM. The Process class is an abstract class, because a specific subclass of Process exists for each operating system.

You can pass three possible input parameters into these methods:

A single string that represents both the program to execute and any arguments to that program
An array of strings that separate the program from its arguments
An array of environment variables

Pass in the environment variables in the form name=value. If you use the version of exec() with a single string for both the program and its arguments, note that the string is parsed using white space as the delimiter via the StringTokenizer class.

Stumbling into an IllegalThreadStateException

The first pitfall relating to Runtime.exec() is the IllegalThreadStateException. The prevalent first test of an API is to code its most obvious methods. For example, to execute a process that is external to the Java VM, we use the exec() method. To see the value that the external process returns, we use the exitValue() method on the Process class. In our first example, we will attempt to execute the Java compiler (javac.exe):

Listing 4.1 BadExecJavac.java

import java.util.*;
import java.io.*;
public class BadExecJavac
{
public static void main(String args[])
{
try
{
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec(“javac”);
int exitVal = proc.exitValue();
System.out.println(“Process exitValue: ” + exitVal);
} catch (Throwable t)
{
t.printStackTrace();
}
}
}

InfoWorld: Modernizing IT
JavaWorld: Solutions for Java Developers

Research Centers
Core Java
Enterprise Java
Mobile Java
Tools & Methods
JavaWorld Archives
Site Resources
Featured Articles
News & Views
Community
Java Q&A
JW Blogs
Podcasts
Site Map
Careers
Newsletters
Whitepapers
RSS Feeds
About JavaWorld
Advertise
Write for JW

JW’s Most Read
Recent:

Java Tips: The Serialization algorithm revealed
Know your Oracle application server
SwingX, JRuby: Survivors?
Cloud-ready, multicore-friendly apps, Part 2
Lean service architectures with Java EE 6
Archives:

Hello, OSGi: Bundles for beginners (2008)
Sockets programming in Java: A tutorial (1996)
Understanding JPA, Part 1 (2008)
Smartly load your properties (2003)
REST for Java developers, Part 1 (2008)

Featured White Papers

How to Choose a Web Application Monitoring Solution: Evaluation Guide & Checklist
Newsletter sign-upView all newsletters
Sign up for our technology specific newsletters.

Enterprise Java
Email Address:
Sponsored Links
Free Access to SAP BI Resource Center
Empower your users to discover how to explore business at the speed of thought.
Buy a link now

When Runtime.exec() won’t
Navigate yourself around pitfalls related to the Runtime.exec() method
By Michael C. Daconta, JavaWorld.com, 12/29/00

PrintEmailFeedbackResourcesDiscuss (25)DiggRedditSlashDotStumbledel.icio.usTechnoratidzone As part of the Java language, the java.lang package is implicitly imported into every Java program. This package’s pitfalls surface often, affecting most programmers. This month, I’ll discuss the traps lurking in the Runtime.exec() method.

Pitfall 4: When Runtime.exec() won’t
The class java.lang.Runtime features a static method called getRuntime(), which retrieves the current Java Runtime Environment. That is the only way to obtain a reference to the Runtime object. With that reference, you can run external programs by invoking the Runtime class’s exec() method. Developers often call this method to launch a browser for displaying a help page in HTML.

There are four overloaded versions of the exec() command:

public Process exec(String command);
public Process exec(String [] cmdArray);
public Process exec(String command, String [] envp);
public Process exec(String [] cmdArray, String [] envp);

For each of these methods, a command — and possibly a set of arguments — is passed to an operating-system-specific function call. This subsequently creates an operating-system-specific process (a running program) with a reference to a Process class returned to the Java VM. The Process class is an abstract class, because a specific subclass of Process exists for each operating system.

You can pass three possible input parameters into these methods:

A single string that represents both the program to execute and any arguments to that program
An array of strings that separate the program from its arguments
An array of environment variables

Pass in the environment variables in the form name=value. If you use the version of exec() with a single string for both the program and its arguments, note that the string is parsed using white space as the delimiter via the StringTokenizer class.

Stumbling into an IllegalThreadStateException

The first pitfall relating to Runtime.exec() is the IllegalThreadStateException. The prevalent first test of an API is to code its most obvious methods. For example, to execute a process that is external to the Java VM, we use the exec() method. To see the value that the external process returns, we use the exitValue() method on the Process class. In our first example, we will attempt to execute the Java compiler (javac.exe):

Listing 4.1 BadExecJavac.java

import java.util.*;
import java.io.*;
public class BadExecJavac
{
public static void main(String args[])
{
try
{
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec(“javac”);
int exitVal = proc.exitValue();
System.out.println(“Process exitValue: ” + exitVal);
} catch (Throwable t)
{
t.printStackTrace();
}
}
}

A run of BadExecJavac produces:

E:\classes\com\javaworld\jpitfalls\article2>java BadExecJavac
java.lang.IllegalThreadStateException: process has not exited
at java.lang.Win32Process.exitValue(Native Method)
at BadExecJavac.main(BadExecJavac.java:13)

If an external process has not yet completed, the exitValue() method will throw an IllegalThreadStateException; that’s why this program failed. While the documentation states this fact, why can’t this method wait until it can give a valid answer?

A more thorough look at the methods available in the Process class reveals a waitFor() method that does precisely that. In fact, waitFor() also returns the exit value, which means that you would not use exitValue() and waitFor() in conjunction with each other, but rather would choose one or the other. The only possible time you would use exitValue() instead of waitFor() would be when you don’t want your program to block waiting on an external process that may never complete. Instead of using the waitFor() method, I would prefer passing a boolean parameter called waitFor into the exitValue() method to determine whether or not the current thread should wait. A boolean would be more beneficial because exitValue() is a more appropriate name for this method, and it isn’t necessary for two methods to perform the same function under different conditions. Such simple condition discrimination is the domain of an input parameter.

Therefore, to avoid this trap, either catch the IllegalThreadStateException or wait for the process to complete.

Now, let’s fix the problem in Listing 4.1 and wait for the process to complete. In Listing 4.2, the program again attempts to execute javac.exe and then waits for the external process to complete:

Listing 4.2 BadExecJavac2.java

import java.util.*;
import java.io.*;
public class BadExecJavac2
{
public static void main(String args[])
{
try
{
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec(“javac”);
int exitVal = proc.waitFor();
System.out.println(“Process exitValue: ” + exitVal);
} catch (Throwable t)
{
t.printStackTrace();
}
}
}

Unfortunately, a run of BadExecJavac2 produces no output. The program hangs and never completes. Why does the javac process never complete?

Why Runtime.exec() hangs

The JDK’s Javadoc documentation provides the answer to this question:

Because some native platforms only provide limited buffer size for standard input and output streams, failure to promptly write the input stream or read the output stream of the subprocess may cause the subprocess to block, and even deadlock.

Is this just a case of programmers not reading the documentation, as implied in the oft-quoted advice: read the fine manual (RTFM)? The answer is partially yes. In this case, reading the Javadoc would get you halfway there; it explains that you need to handle the streams to your external process, but it does not tell you how.

Another variable is at play here, as is evident by the large number of programmer questions and misconceptions concerning this API in the newsgroups: though Runtime.exec() and the Process APIs seem extremely simple, that simplicity is deceiving because the simple, or obvious, use of the API is prone to error. The lesson here for the API designer is to reserve simple APIs for simple operations. Operations prone to complexities and platform-specific dependencies should reflect the domain accurately. It is possible for an abstraction to be carried too far. The JConfig library provides an example of a more complete API to handle file and process operations (see Resources below for more information).

Now, let’s follow the JDK documentation and handle the output of the javac process. When you run javac without any arguments, it produces a set of usage statements that describe how to run the program and the meaning of all the available program options. Knowing that this is going to the stderr stream, you can easily write a program to exhaust that stream before waiting for the process to exit. Listing 4.3 completes that task. While this approach will work, it is not a good general solution. Thus, Listing 4.3′s program is named MediocreExecJavac; it provides only a mediocre solution. A better solution would empty both the standard error stream and the standard output stream. And the best solution would empty these streams simultaneously (I’ll demonstrate that later).

Listing 4.3 MediocreExecJavac.java

import java.util.*;
import java.io.*;
public class MediocreExecJavac
{
public static void main(String args[])
{
try
{
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec(“javac”);
InputStream stderr = proc.getErrorStream();
InputStreamReader isr = new InputStreamReader(stderr);
BufferedReader br = new BufferedReader(isr);
String line = null;
System.out.println(“”);
while ( (line = br.readLine()) != null)
System.out.println(line);
System.out.println(“”);
int exitVal = proc.waitFor();
System.out.println(“Process exitValue: ” + exitVal);
} catch (Throwable t)
{
t.printStackTrace();
}
}
}

A run of MediocreExecJavac generates:

E:\classes\com\javaworld\jpitfalls\article2>java MediocreExecJavac

Usage: javac
where includes:
-g Generate all debugging info
-g:none Generate no debugging info
-g:{lines,vars,source} Generate only some debugging info
-O Optimize; may hinder debugging or enlarge class files
-nowarn Generate no warnings
-verbose Output messages about what the compiler is doing
-deprecation Output source locations where deprecated APIs are used
-classpath Specify where to find user class files
-sourcepath Specify where to find input source files
-bootclasspath Override location of bootstrap class files
-extdirs Override location of installed extensions
-d Specify where to place generated class files
-encoding Specify character encoding used by source files
-target Generate class files for specific VM version

Process exitValue: 2

So, MediocreExecJavac works and produces an exit value of 2. Normally, an exit value of 0 indicates success; any nonzero value indicates an error. The meaning of these exit values depends on the particular operating system. A Win32 error with a value of 2 is a “file not found” error. That makes sense, since javac expects us to follow the program with the source code file to compile.

Thus, to circumvent the second pitfall — hanging forever in Runtime.exec() — if the program you launch produces output or expects input, ensure that you process the input and output streams.

Assuming a command is an executable program

Under the Windows operating system, many new programmers stumble upon Runtime.exec() when trying to use it for nonexecutable commands like dir and copy. Subsequently, they run into Runtime.exec()’s third pitfall. Listing 4.4 demonstrates exactly that:

Listing 4.4 BadExecWinDir.java

import java.util.*;
import java.io.*;
public class BadExecWinDir
{
public static void main(String args[])
{
try
{
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec(“dir”);
InputStream stdin = proc.getInputStream();
InputStreamReader isr = new InputStreamReader(stdin);
BufferedReader br = new BufferedReader(isr);
String line = null;
System.out.println(“”);
while ( (line = br.readLine()) != null)
System.out.println(line);
System.out.println(“”);
int exitVal = proc.waitFor();
System.out.println(“Process exitValue: ” + exitVal);
} catch (Throwable t)
{
t.printStackTrace();
}
}
}

A run of BadExecWinDir produces:

E:\classes\com\javaworld\jpitfalls\article2>java BadExecWinDir
java.io.IOException: CreateProcess: dir error=2
at java.lang.Win32Process.create(Native Method)
at java.lang.Win32Process.(Unknown Source)
at java.lang.Runtime.execInternal(Native Method)
at java.lang.Runtime.exec(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at BadExecWinDir.main(BadExecWinDir.java:12)

As stated earlier, the error value of 2 means “file not found,” which, in this case, means that the executable named dir.exe could not be found. That’s because the directory command is part of the Windows command interpreter and not a separate executable. To run the Windows command interpreter, execute either command.com or cmd.exe, depending on the Windows operating system you use. Listing 4.5 runs a copy of the Windows command interpreter and then executes the user-supplied command (e.g., dir).

Listing 4.5 GoodWindowsExec.java

import java.util.*;
import java.io.*;
class StreamGobbler extends Thread
{
InputStream is;
String type;

StreamGobbler(InputStream is, String type)
{
this.is = is;
this.type = type;
}

public void run()
{
try
{
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line=null;
while ( (line = br.readLine()) != null)
System.out.println(type + “>” + line);
} catch (IOException ioe)
{
ioe.printStackTrace();
}
}
}
public class GoodWindowsExec
{
public static void main(String args[])
{
if (args.length < 1)
{
System.out.println("USAGE: java GoodWindowsExec “);
System.exit(1);
}

try
{
String osName = System.getProperty(“os.name” );
String[] cmd = new String[3];
if( osName.equals( “Windows NT” ) )
{
cmd[0] = “cmd.exe” ;
cmd[1] = “/C” ;
cmd[2] = args[0];
}
else if( osName.equals( “Windows 95″ ) )
{
cmd[0] = “command.com” ;
cmd[1] = “/C” ;
cmd[2] = args[0];
}

Runtime rt = Runtime.getRuntime();
System.out.println(“Execing ” + cmd[0] + ” ” + cmd[1]
+ ” ” + cmd[2]);
Process proc = rt.exec(cmd);
// any error message?
StreamGobbler errorGobbler = new
StreamGobbler(proc.getErrorStream(), “ERROR”);

// any output?
StreamGobbler outputGobbler = new
StreamGobbler(proc.getInputStream(), “OUTPUT”);

// kick them off
errorGobbler.start();
outputGobbler.start();

// any error???
int exitVal = proc.waitFor();
System.out.println(“ExitValue: ” + exitVal);
} catch (Throwable t)
{
t.printStackTrace();
}
}
}

Running GoodWindowsExec with the dir command generates:

E:\classes\com\javaworld\jpitfalls\article2>java GoodWindowsExec “dir *.java”
Execing cmd.exe /C dir *.java
OUTPUT> Volume in drive E has no label.
OUTPUT> Volume Serial Number is 5C5F-0CC9
OUTPUT>
OUTPUT> Directory of E:\classes\com\javaworld\jpitfalls\article2
OUTPUT>
OUTPUT>10/23/00 09:01p 805 BadExecBrowser.java
OUTPUT>10/22/00 09:35a 770 BadExecBrowser1.java
OUTPUT>10/24/00 08:45p 488 BadExecJavac.java
OUTPUT>10/24/00 08:46p 519 BadExecJavac2.java
OUTPUT>10/24/00 09:13p 930 BadExecWinDir.java
OUTPUT>10/22/00 09:21a 2,282 BadURLPost.java
OUTPUT>10/22/00 09:20a 2,273 BadURLPost1.java
… (some output omitted for brevity)
OUTPUT>10/12/00 09:29p 151 SuperFrame.java
OUTPUT>10/24/00 09:23p 1,814 TestExec.java
OUTPUT>10/09/00 05:47p 23,543 TestStringReplace.java
OUTPUT>10/12/00 08:55p 228 TopLevel.java
OUTPUT> 22 File(s) 46,661 bytes
OUTPUT> 19,678,420,992 bytes free
ExitValue: 0

Running GoodWindowsExec with any associated document type will launch the application associated with that document type. For example, to launch Microsoft Word to display a Word document (i.e., one with a .doc extension), type:

>java GoodWindowsExec “yourdoc.doc”

Notice that GoodWindowsExec uses the os.name system property to determine which Windows operating system you are running — and thus determine the appropriate command interpreter. After executing the command interpreter, handle the standard error and standard input streams with the StreamGobbler class. StreamGobbler empties any stream passed into it in a separate thread. The class uses a simple String type to denote the stream it empties when it prints the line just read to the console.

Thus, to avoid the third pitfall related to Runtime.exec(), do not assume that a command is an executable program; know whether you are executing a standalone executable or an interpreted command. At the end of this section, I will demonstrate a simple command-line tool that will help you with that analysis.

It is important to note that the method used to obtain a process’s output stream is called getInputStream(). The thing to remember is that the API sees things from the perspective of the Java program and not the external process. Therefore, the external program’s output is the Java program’s input. And that logic carries over to the external program’s input stream, which is an output stream to the Java program.

Runtime.exec() is not a command line

One final pitfall to cover with Runtime.exec() is mistakenly assuming that exec() accepts any String that your command line (or shell) accepts. Runtime.exec() is much more limited and not cross-platform. This pitfall is caused by users attempting to use the exec() method to accept a single String as a command line would. The confusion may be due to the fact that command is the parameter name for the exec() method. Thus, the programmer incorrectly associates the parameter command with anything that he or she can type on a command line, instead of associating it with a single program and its arguments. In listing 4.6 below, a user tries to execute a command and redirect its output in one call to exec():

Listing 4.6 BadWinRedirect.java

import java.util.*;
import java.io.*;
// StreamGobbler omitted for brevity
public class BadWinRedirect
{
public static void main(String args[])
{
try
{
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec(“java jecho ‘Hello World’ > test.txt”);
// any error message?
StreamGobbler errorGobbler = new
StreamGobbler(proc.getErrorStream(), “ERROR”);

// any output?
StreamGobbler outputGobbler = new
StreamGobbler(proc.getInputStream(), “OUTPUT”);

// kick them off
errorGobbler.start();
outputGobbler.start();

// any error???
int exitVal = proc.waitFor();
System.out.println(“ExitValue: ” + exitVal);
} catch (Throwable t)
{
t.printStackTrace();
}
}
}

Running BadWinRedirect produces:

E:\classes\com\javaworld\jpitfalls\article2>java BadWinRedirect
OUTPUT>’Hello World’ > test.txt
ExitValue: 0

The program BadWinRedirect attempted to redirect the output of an echo program’s simple Java version into the file test.txt. However, we find that the file test.txt does not exist. The jecho program simply takes its command-line arguments and writes them to the standard output stream. (You will find the source for jecho in the source code available for download in Resources.) In Listing 4.6, the user assumed that you could redirect standard output into a file just as you could on a DOS command line. Nevertheless, you do not redirect the output through this approach. The incorrect assumption here is that the exec() method acts like a shell interpreter; it does not. Instead, exec() executes a single executable (a program or script). If you want to process the stream to either redirect it or pipe it into another program, you must do so programmatically, using the java.io package. Listing 4.7 properly redirects the standard output stream of the jecho process into a file.

Listing 4.7 GoodWinRedirect.java

import java.util.*;
import java.io.*;
class StreamGobbler extends Thread
{
InputStream is;
String type;
OutputStream os;

StreamGobbler(InputStream is, String type)
{
this(is, type, null);
}
StreamGobbler(InputStream is, String type, OutputStream redirect)
{
this.is = is;
this.type = type;
this.os = redirect;
}

public void run()
{
try
{
PrintWriter pw = null;
if (os != null)
pw = new PrintWriter(os);

InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line=null;
while ( (line = br.readLine()) != null)
{
if (pw != null)
pw.println(line);
System.out.println(type + “>” + line);
}
if (pw != null)
pw.flush();
} catch (IOException ioe)
{
ioe.printStackTrace();
}
}
}
public class GoodWinRedirect
{
public static void main(String args[])
{
if (args.length < 1)
{
System.out.println("USAGE java GoodWinRedirect “);
System.exit(1);
}

try
{
FileOutputStream fos = new FileOutputStream(args[0]);
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec(“java jecho ‘Hello World’”);
// any error message?
StreamGobbler errorGobbler = new
StreamGobbler(proc.getErrorStream(), “ERROR”);

// any output?
StreamGobbler outputGobbler = new
StreamGobbler(proc.getInputStream(), “OUTPUT”, fos);

// kick them off
errorGobbler.start();
outputGobbler.start();

// any error???
int exitVal = proc.waitFor();
System.out.println(“ExitValue: ” + exitVal);
fos.flush();
fos.close();
} catch (Throwable t)
{
t.printStackTrace();
}
}
}

Running GoodWinRedirect produces:

E:\classes\com\javaworld\jpitfalls\article2>java GoodWinRedirect test.txt
OUTPUT>’Hello World’
ExitValue: 0

After running GoodWinRedirect, test.txt does exist. The solution to the pitfall was to simply control the redirection by handling the external process’s standard output stream separately from the Runtime.exec() method. We create a separate OutputStream, read in the filename to which we redirect the output, open the file, and write the output that we receive from the spawned process’s standard output to the file. Listing 4.7 completes that task by adding a new constructor to our StreamGobbler class. The new constructor takes three arguments: the input stream to gobble, the type String that labels the stream we are gobbling, and the output stream to which we redirect the input. This new version of StreamGobbler does not break any of the code in which it was previously used, as we have not changed the existing public API — we only extended it.

Since the argument to Runtime.exec() is dependent on the operating system, the proper commands to use will vary from one OS to another. So, before finalizing arguments to Runtime.exec() and writing the code, quickly test the arguments. Listing 4.8 is a simple command-line utility that allows you to do just that.

Here’s a useful exercise: try to modify TestExec to redirect the standard input or standard output to a file. When executing the javac compiler on Windows 95 or Windows 98, that would solve the problem of error messages scrolling off the top of the limited command-line buffer.

Listing 4.8 TestExec.java

import java.util.*;
import java.io.*;
// class StreamGobbler omitted for brevity
public class TestExec
{
public static void main(String args[])
{
if (args.length java TestExec “e:\java\docs\index.html”
java.io.IOException: CreateProcess: e:\java\docs\index.html error=193
at java.lang.Win32Process.create(Native Method)
at java.lang.Win32Process.(Unknown Source)
at java.lang.Runtime.execInternal(Native Method)
at java.lang.Runtime.exec(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at TestExec.main(TestExec.java:45)

Our first test failed with an error of 193. The Win32 error for value 193 is “not a valid Win32 application.” This error tells us that no path to an associated application (e.g., Netscape) exists, and that the process cannot run an HTML file without an associated application.

Therefore, we try the test again, this time giving it a full path to Netscape. (Alternately, we could add Netscape to our PATH environment variable.) A second run of TestExec produces:

E:\classes\com\javaworld\jpitfalls\article2>java TestExec
“e:\program files\netscape\program\netscape.exe e:\java\docs\index.html”
ExitValue: 0

This worked! The Netscape browser launches, and it then loads the Java help documentation.

One additional improvement to TestExec would include a command-line switch to accept input from standard input. You would then use the Process.getOutputStream() method to pass the input to the spawned external program.

To sum up, follow these rules of thumb to avoid the pitfalls in Runtime.exec():

You cannot obtain an exit status from an external process until it has exited
You must immediately handle the input, output, and error streams from your spawned external process
You must use Runtime.exec() to execute programs
You cannot use Runtime.exec() like a command line

Correction to Pitfall 3
In the discussion of Pitfall 3 (“Don’t mix floats and doubles when generating text or XML messages”) in my last column, I incorrectly stated that the different string representation of a decimal number after casting it from a float to a double was a bug. While this is a pitfall, its cause is not a bug, but the fact that the decimal numbers in question — 100.28 and 91.09 — do not represent precisely in binary. I’d like to thank Thomas Okken and the others who straightened me out. If you enjoy discussing the finer points of numerical methods, you can email Thomas.

The combination of forgetting my numerical methods class, the numerous bug reports on the bug parade, and the automatic rounding of floats and doubles when printing (but not after casting a float to a double) threw me. I apologize for confusing anyone who read the article, especially to new Java programmers. I present two better solutions to the problem:

The first possible solution is to always specify the desired rounding explicitly with NumberFormat. In my case, I use the float and double to represent dollars and cents; therefore, I need only two significant digits. Listing C3.1 demonstrates how to use the NumberFormat class to specify a maximum of two fraction digits.

Listing C3.1 FormatNumbers.java

import java.text.*;
public class FormatNumbers
{
public static void main(String [] args)
{
try
{
NumberFormat fmt = NumberFormat.getInstance();
fmt.setMaximumFractionDigits(2);
float f = 100.28f;
System.out.println(“As a float : ” + f);
double d = f;
System.out.println(“Cast to a double : ” + d);
System.out.println(“Using NumberFormat: ” + fmt.format(d));
} catch (Throwable t)
{
t.printStackTrace();
}
}
}

When we run the FormatNumbers program, it produces:

E:\classes\com\javaworld\jpitfalls\article2>java FormatNumbers
As a float : 100.28
Cast to a double : 100.27999877929688
Using NumberFormat: 100.28

As you can see — regardless of whether we cast the float to a double — when we specify the number of digits we want, it properly rounds to that precision — even if the number is infinitely repeating in binary. To circumvent this pitfall, control the formatting of your doubles and floats when converting to a String.

A second, simpler solution would be to not use a float to represent cents. Integers (number of pennies) can represent cents, with a legal range of 0 to 99. You can check the range in the mutator method.

Qouted from an Article Whose author is :
Michael C. Daconta is the director of Web and technology services for McDonald Bradley, where he conducts training seminars and develops advanced systems with Java, JavaScript, and XML. Over the past 15 years, Daconta has held every major development position, including chief scientist, technical director, chief developer, team leader, systems analyst, and programmer. He is a Sun-certified Java programmer and coauthor of Java Pitfalls (John Wiley & Sons, 2000), Java 2 and JavaScript for C and C++ Programmers (John Wiley & Sons, 1999), and XML Development with Java 2 (Sams Publishing, 2000). In addition, he is the author of C++ Pointers and Dynamic Memory Management (John Wiley & Sons, 1995).

Older Posts »

Follow

Get every new post delivered to your Inbox.