16 January, 2008

Java processes viewer

There is a way how to retrieve information about all running JVMs.
Since Java 1.5 Sun provides sources of jvmstat classes for JVM monitoring.

I've created simple program that is used these classes.
It detects all running JVMs and prints their information.



JavaPS.java


import java.util.Set;
import sun.jvmstat.monitor.HostIdentifier;
import sun.jvmstat.monitor.MonitoredHost;
import sun.jvmstat.monitor.MonitoredVm;
import sun.jvmstat.monitor.StringMonitor;
import sun.jvmstat.monitor.VmIdentifier;

public class JavaPS {
private static final String[] vmProperties = new String[]{
"java.property.java.vm.name",
"java.property.java.vm.vendor",
"java.property.java.vm.version",
"java.property.java.home",
"java.property.java.class.path",
"java.rt.vmArgs",
"java.rt.vmFlags",
"sun.rt.javaCommand"};

public static void main(String[] args) {
Set jvms;
MonitoredHost monitoredHost;

try {
monitoredHost = MonitoredHost.getMonitoredHost(new HostIdentifier("localhost"));
jvms = monitoredHost.activeVms();
} catch (Exception e) {
e.printStackTrace();
return;
}

for (Object jvm : jvms) {
int jvmid = (Integer) jvm;
System.out.println("Process ID: \t" + String.valueOf(jvmid));
try {
VmIdentifier id = new VmIdentifier("//" + jvmid + "?mode=r");
MonitoredVm vm = monitoredHost.getMonitoredVm(id, 0);
for (String vmProperty : vmProperties) {
System.out.println(vmProperty + ": \t"
+ ((StringMonitor) vm.findByName(vmProperty)).stringValue());
}
System.out.println();
monitoredHost.detach(vm);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}


Aliases for virtual machine's properties have been taken from jdk1.5 sources (jdk-1_5_0-src-scsl.zip) from the file j2se\src\share\classes\sun\jvmstat\perfdata\resources\aliasmap.

Compile this code via command:
javac -classpath %JAVA_HOME%\lib\tools.jar JavaPS.java
and run:
java -classpath .;%JAVA_HOME%\lib\tools.jar JavaPS >JavaPS.txt

jvmstat package locates in tools.jar


Example of results output:


JavaPS.txt

Process ID: 2144
java.property.java.vm.name: Java HotSpot(TM) Client VM
java.property.java.vm.vendor: Sun Microsystems Inc.
java.property.java.vm.version: 1.6.0_03-b05
java.property.java.home: C:\Program Files\Java\jre1.6.0_03
java.property.java.class.path: .;E:\Devel\jdk1.5.0_09\lib\tools.jar
java.rt.vmArgs:
java.rt.vmFlags:
sun.rt.javaCommand: JavaPS

Process ID: 364
java.property.java.vm.name: Java HotSpot(TM) Client VM
java.property.java.vm.vendor: Sun Microsystems Inc.
java.property.java.vm.version: 1.6.0_03-b05
java.property.java.home: C:\Program Files\Java\jre1.6.0_03
java.property.java.class.path: SwingSet2.jar
java.rt.vmArgs:
java.rt.vmFlags:
sun.rt.javaCommand: SwingSet2.jar

Simple RTF to XML converter

RTFEditorKit (javax.swing.text.rtf.RTFEditorKit) from Sun Java API - special class for operations with RTF (Rich Text Format) documents.

I've created java sample that converts RTF document to XML.

This is the source of this converter:



Rtf2XML.java import javax.swing.text.AbstractDocument.BranchElement; import javax.swing.text.DefaultStyledDocument; import javax.swing.text.BadLocationException; import javax.swing.text.rtf.RTFEditorKit; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.UnsupportedEncodingException; public class Rtf2XML { private DefaultStyledDocument rtfSource; private org.w3c.dom.Document xmlTarget; private org.w3c.dom.Element xmlRoot; private void expandElement(javax.swing.text.Element rtfElement) { for (int i = 0; i < rtfElement.getElementCount(); i++) { javax.swing.text.Element rtfNextElement = rtfElement.getElement(i); if (rtfNextElement.isLeaf()) { try { addElement(rtfNextElement); } catch (Exception e) { e.printStackTrace(); } } else { expandElement(rtfNextElement); } } } private void addElement(javax.swing.text.Element rtfElement) throws UnsupportedEncodingException, BadLocationException { String style = new String(rtfSource.getLogicalStyle(rtfElement.getStartOffset()) .getName().getBytes("ISO-8859-1")); String text = new String(rtfSource.getText(rtfElement.getStartOffset(), rtfElement.getEndOffset() - rtfElement.getStartOffset()) .getBytes("ISO-8859-1")); org.w3c.dom.Element node = xmlTarget.createElement("p"); node.appendChild(xmlTarget.createTextNode(text)); node.setAttribute("style", style); xmlRoot.appendChild(node); } public void convert(String sourceFileName) throws Exception { rtfSource = new DefaultStyledDocument(); RTFEditorKit kit = new RTFEditorKit(); kit.read(new FileInputStream(sourceFileName), rtfSource, 0); xmlTarget = DocumentBuilderFactory.newInstance() .newDocumentBuilder().newDocument(); BranchElement rtfRoot = (BranchElement) rtfSource.getDefaultRootElement(); xmlRoot = xmlTarget.createElement("data"); expandElement(rtfRoot); xmlTarget.appendChild(xmlRoot); Transformer t = TransformerFactory.newInstance().newTransformer(); t.transform(new DOMSource(xmlTarget), new StreamResult(new FileOutputStream(sourceFileName + ".xml"))); } public static void main(String[] args) { if (args.length != 1) { System.err.println("Usage: *.rtf"); return; } try { new Rtf2XML().convert(args[0]); } catch (Exception e) { e.printStackTrace(); } } }


But RTFEditorKit isn't so powerful and friendly as I want.
I think, iText will be better for operations with RTF (and other document formats).

It's the good and free decision.

10 January, 2008

Simple Java MIDI synthesizer sample

Another multi-media sample - simplest Java MIDI synthesizer.


MidiSynthesizerSample.java

import javax.sound.midi.*;
 
public class MidiSynthesizerSample {
  public static void main(String[] args) {
      int[] notes = new int[]{60, 62, 64, 65, 67, 69, 71, 72, 72, 71, 69, 67, 65, 64, 62, 60};
      try {
          Synthesizer synthesizer = MidiSystem.getSynthesizer();
          synthesizer.open();
          MidiChannel channel = synthesizer.getChannels()[0];
 
          for (int note : notes) {
              channel.noteOn(note, 50);
              try {
                  Thread.sleep(200);
              } catch (InterruptedException e) {
                  break;
              } finally {
                  channel.noteOff(note);
              }
          }
      } catch (MidiUnavailableException e) {
          e.printStackTrace();
      }
  }
}


See example of Java MIDI application:
XenoHarmonica

09 January, 2008

Simple Java MIDI player sample

New Year!

XenoHarmonica
- is a musical project. It is a bayan keyboard emulator, Java MIDI application for personal computers.
Now everybody can play bayan (button accordion).

XenoHarmonica is free for education and non-commercial usage.

Program based on Java MIDI API.
In this article I provide an example of simplest MIDI player.

Maybe somebody else going to create MIDI program :-)



MidiPlayerSample.java
package xantorohara.xenoharmonica.samples;
 
import javax.sound.midi.MidiSystem;
import javax.sound.midi.MidiUnavailableException;
import javax.sound.midi.Sequence;
import javax.sound.midi.Sequencer;
import java.io.FileInputStream;
 
public class MidiPlayerSample {
 
    public static void main(String[] args) {
        try {
            Sequencer sequencer = MidiSystem.getSequencer();
            if (sequencer == null)
                throw new MidiUnavailableException();
            sequencer.open();
            FileInputStream is = new FileInputStream("sample.mid");
            Sequence mySeq = MidiSystem.getSequence(is);
            sequencer.setSequence(mySeq);
            sequencer.start();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
 

Windows XP hibernate mode problem

Do you have a problem with Hibernate mode in Windows XP?
And you can't turn on Hibernate mode from Power Options Panel, it says: "The process cannot access the file because it is being used by another process".
I found one crasy method how to resolve this problem on my computer (AMD64/1G RAM/MB Asus M2NE/Video Asus N7600/IDE HDD).

  • Delete file: WINDOWS\system32\drivers\atapi.sys (it will be restored by Windows).

  • Manualy enable hibernate mode (now it isn't throw error message).

  • Switch computer to Hibernate mode.


Is it helpful?