Jump to content United States-English
HP.com Home Products and Services Support and Drivers Solutions How to Buy
» Contact HP
More options
HP.com home
Java™ Troubleshooting Guide for HP-UX Systems: > Chapter 4 Core File Analysis

Sample Java Application

» 

Technical documentation

Complete book in PDF
» Feedback
Content starts here

 » Table of Contents

 » Glossary

 » Index

This sample application contains native code (C) and Java code. This particular application aborts in native code because this code contains a defect. The defect causes a runtime failure, which results in a core dump.

The example consists of three files for an application named StackTrace:

These three files should be placed in a directory named StackTrace.

The following subsections contain listings of each of these three files. This example is run on a PA-RISC system. However, it can be tested on any PA-RISC or Integrity system though, if some changes are made to the StackTraceJob script.

StackTraceJob

#!/bin/ksh  

# set JAVA_HOME
export JAVA_HOME=/opt/java1.4  

# set PATH 
export PATH=$JAVA_HOME/bin:$PATH

# Compile java bytecode 
/usr/bin/echo "Compile java code" 
javac StackTrace.java

# create header file 
/usr/bin/echo "Create header file" 
javah -verbose -jni StackTrace  

# compile jni code 
/usr/bin/echo "Compile c code"
/usr/bin/cc +z -c -I $JAVA_HOME/include \ 
                  -I $JAVA_HOME/include/hp-ux stacktrace.c  

# create shared library 
/usr/bin/echo "Create shared library" 
/usr/bin/ld -b -o libstacktrace.sl stacktrace.o   

/usr/bin/echo "Run StackTrace program" 
export SHLIB_PATH=. 
export LD_LIBRARY_PATH=. 

java StackTrace
NOTE: If this script is run on an Integrity system, change it from:
# create shared library
/usr/bin/echo "Create shared library" 
/usr/bin/ld -b -o libstacktrace.sl stacktrace.o 
to:
# create shared library 
/usr/bin/echo "Create shared library" 
/usr/bin/ld -b -o libstacktrace.so stacktrace.o 

StackTrace.java

// File StackTrace.java

public class StackTrace {
  native static String dumpCore(int i);
  
//**************************************************
  public static void method1(int ci) {
    System.out.println("Calling method2()");
    StackTrace.method2(ci);   
  }    // end method1

//**************************************************
  public static void method2(int ci) {
    System.out.println("Calling method3()");
    StackTrace.method3(ci);   
  }    // end method2  

//**************************************************
  public static void method3(int ci) {
    System.out.println("Calling methodMakeCall()");
    StackTrace.methodMakeCall(ci);  
  }    // end method3  

//**************************************************    
public static void methodMakeCall(int ci) {
  try { 
    System.loadLibrary("stacktrace");
  }     
  catch (UnsatisfiedLinkError Err) { 
    System.out.println("error: " + Err);
    System.exit(1);     
  }     

  System.out.println("Call dumpCore to convert " + ci +
        " to a binary string!"); 
  System.out.println("The binary String: " + StackTrace.dumpCore(ci));
}    // end methodMakeCall  

//**************************************************
  public static void main(String args[]) {
    int convertInt;

    System.out.println();
    if(args.length == 1) {
      convertInt = Integer.parseInt(args[0]); 
    }     
    else { 
      convertInt = 757;
    }
    System.out.println("Calling method1()"); 
    StackTrace.method1(convertInt);
    System.out.println("Back in main, all done!");    
  }    // end main    
}      // end StackTrace  

stacktrace.c

// File stacktrace.c 

#include "StackTrace.h"  
#include <stdio.h> 

JNIEXPORT jstring JNICALL 
  Java_StackTrace_dumpCore(JNIEnv *env, jclass class, jint intarg) { 

  jclass     classid; 
  jmethodID  methodid; 

  printf("In dumpCore\n");

// Problem code.  The Class:  java.lang.IntegerX does not exist, 
// but the exception is cleared and the program continues.
// Ultimately, failing and dumping core when getting the methodid.
// To fix, comment out the following 2 lines. 
// /* 
  classid = (*env)->FindClass(env, "java/lang/IntegerX");
  (*env)->ExceptionClear(env); 
// */  

// Working code.  The Class:  java.lang.Integer exists.  The lines 
// following the FindClass manage printing out of a stack trace, 
// clearing an exception, and returning to the java main when  
// FindClass fails.
// 
// Uncomment the following 6 lines and rebuild to see the program work! 
 /*   
  classid = (*env)->FindClass(env, "java/lang/Integer");
  (*env)->ExceptionDescribe(env); 
  (*env)->ExceptionClear(env); 
  if(classid == NULL) { 
    return (*env)->NewStringUTF(env, "JNI FindClass failed!");
  } 
 */    
  
  methodid = (*env)->GetStaticMethodID(env, classid, "toBinaryString", 
                     "(I)Ljava/lang/String;"); 
  (*env)->ExceptionDescribe(env); 
  (*env)->ExceptionClear(env);  
  if(methodid == NULL) { 
    return (*env)->NewStringUTF(env, "JNI GetStaticMethodID failed!");  
  }  

  return (*env)->CallStaticObjectMethod(env, classid, methodid, intarg); 

} 
Printable version
Privacy statement Using this site means you accept its terms Feedback to webmaster
© 2009 Hewlett-Packard Development Company, L.P.