Welcome to my website. I am always posting links to photo albums, art, technology and other creations. Everything that you will see on my numerous personal sites is powered by the formVistaTM Website Management Engine.

icon.linkedin.jpgicon.twitter.jpg

  • Subscribe to this RSS Feed
  • Java Project Template
    08/11/2012 9:10AM

    Attached to this post is a tar.gz file that contains a complete template with which you can build a Java project.  It includes the directory structure, sample ant build.xml, logging configs, and shell scripts (and the beginnings of some Windows scripts) to run the program:


    java_project_template/README.txt
    java_project_template/conf/
    java_project_template/conf/log4j.properties
    java_project_template/lib/
    java_project_template/lib/log4j-1.2.16.jar
    java_project_template/launch.sh
    java_project_template/scripts/
    java_project_template/scripts/run.sh
    java_project_template/scripts/run.vbs
    java_project_template/scripts/run.bat
    java_project_template/src/
    java_project_template/src/com/
    java_project_template/src/com/ryanchapin/
    java_project_template/src/com/ryanchapin/samples/
    java_project_template/src/com/ryanchapin/samples/java/
    java_project_template/src/com/ryanchapin/samples/java/Example.java

    The ant build file will create a build/classes directory into which all of the Java .class files are compiled.  It will also create a dist directory into which all of the jars, configs, and scripts to run the program are placed.

    The run.sh script will dynamically concatenate the classpath argument for java based on the contents of the dist/lib/ directory and can be customized to accept any additional command line arguments that you may require.

    The conf/ directory includes a log4j.properties file to get you started.

    The build.xml file includes default, clean, and dist targets and does all of the basics needed to build your project.

    The launch.sh script is used primarily during development to invoke run.sh without having to cd into the dist folder each time it is run.

    Attachment: java_project_template.tar.gz 439218 bytes
  • Starting a Process as a Non Root User on Startup under Linux
    07/25/2012 8:52AM

    The following is a quick howto for when you want to fire up a process as a non-root user on boot under Linux:

    Create a script, typically under /usr/bin with something similar to the following:

    #!/bin/bash

    # invoke your process

    /usr/local/process/someprocess

    exit

    Now, add an entry to /etc/rc.d/rc.local to invoke the command as the non-root user

    su - [non-root-user] -c [path-to-shell-script]

    Make sure that the exit command is the last thing in the shell script so that you will exit the su and rc.local will continue processing as the root user.

  • Adding JBoss EJB3 Libraries/JBoss Runtime to the Build Path in Eclipse
    07/23/2012 2:49PM

    When developing JavaEE applications in Eclipse it is more or less necessary to have all of the JavaEE libraries on the build path.

    To do so, do the following:

    • Unpack the version of the JavaEE application server that you are using.  In this case it is JBoss 6.0.0.Final.
    • Run the 'Add a Server' wizard under the JavaEE perspective's Server tab.
    • Add the Server Runtime to the build path/libraries:
    • Right-click on your project and select 'Build Path/Configure Build Path'
    • Click on the 'Libraries' tab
    • Click on 'Add Library'
    • Select 'Server Runtime'
    • Select the JBoss Runtime declared server (from the step above)
  • Too Many Open Files Errors When Using Runtime.exec() or ProcessBuilder.start() to Execute A Process
    07/11/2012 10:58AM
    Java enables you to exectute a process on the host OS and to provide input (STDIN) to the process and read standard out (STDOUT) and standard error (STDERR).  Each time that you execute a process via Runtime.exec() or ProcessBuilder.start() STDIN, STOUT, and STDERR are piped to the running JVM as an OutputStream or InputStream.  For each stream there are two file descriptors and typically when the process terminates those streams are closed also closing the two file descriptors.

    Even if you destroy the Process via Process.destroy() you may still be leaking file descriptors.  The typical symptoms are as follows:

    1. You see a growing number of named pipes in the output of lsof:

    java      8654 rchapin   23w     FIFO        0,6        0t0      26015 pipe
    java      8654 rchapin   24r     FIFO        0,6        0t0      26016 pipe
    java      8654 rchapin   26r     FIFO        0,6        0t0      26017 pipe
    java      8654 rchapin   30w     FIFO        0,6        0t0      26031 pipe
    java      8654 rchapin   31r     FIFO        0,6        0t0      26032 pipe
    java      8654 rchapin   33r     FIFO        0,6        0t0      26033 pipe
    java      8678 rchapin    0r     FIFO        0,6        0t0      26015 pipe
    java      8678 rchapin    1w     FIFO        0,6        0t0      26016 pipe
    java      8678 rchapin    2w     FIFO        0,6        0t0      26017 pipe
    java      8680 rchapin    0r     FIFO        0,6        0t0      26031 pipe
    java      8680 rchapin    1w     FIFO        0,6        0t0      26032 pipe
    java      8680 rchapin    2w     FIFO        0,6        0t0      26033 pipe


    2. You start seeing the 'Too many open files' error in your log:

    2012-07-06 02:39:27,260 (ERROR) (Thread-6) java.io.IOException: error=24, Too many open files

    A good practice is to make sure to get a reference to each of the Streams and to explicitly close them, REGARDLESS of whether you actually utilize those streams.

    Example:

    Process process = Runtime.getRuntime.exec("some command here");
    or
    ProcessBuilder processBuilder = new ProcessBuilder(someCommandArray);
    Process process = processBuilder.start();

    // To close them
    process.getInputStream().close();
    process.getOutputStream().close();
    process.getErrorStream().close();
  • Creating an Eclipse Java Project from an Existing Directory Structure and Source
    05/18/2012 10:29AM

    If you are keeping your code and configs in a repository, more likely than not (and it is recommended) that you do not also store your IDE specific configurations or files within the repo.

    As such, you will only keep the bare minimum of IDE independent files in your repository and when you check out a copy of it you will have an existing set of directories and files that you will use to create a project with in Eclipse.

    To create a new project in Eclipse using the existing directory structure, do the following:

    • In Eclipse go to: File/New/Project
    • Select "Java Project"
    • Un-check "Use default location" and then click Browse and navigate to the top level directory with contains your source, libs, configs, etc.
    • Eclipse will display a warning that says that your project "overlaps the location of another project".
    • Making sure that the "Location" field now contains the path to your source directory, RE-check the "Use default location" checkbox and now Eclipse will let you click on the "Next" button to continue configuration of your project.
Advanced Search

Categories

Archives