Friday, October 07, 2011

Ruby script to list process info in Windows

Below is a Ruby script to list process id and its command line in Windows.

To run it:
ruby pid.rb <process-name>
ie: ruby pid.rb java: will list java.exe and javaw.exe processes


class ProcessInfo
  attr_accessor :cmdLine, :pid
  
  def self.parseWmic(cmd)
    result = `#{cmd}`
    raise("Error: " + result) unless $? == 0
    processes = []
    pinfo = nil
    result.split(/\r?\n/).each do |line|
      next if line =~ /^\s*$/
      if line =~ /CommandLine=(.*)/i
        pinfo = ProcessInfo.new
        pinfo.cmdLine = $1
      elsif line =~ /ProcessId=(\d+)/i
        pinfo.pid = $1
        processes << pinfo unless pinfo.pid.to_i == $$.to_i
      end
    end
    return processes
  end
  
  def self.queryProcess(processName)
    return self.parseWmic("wmic process where \"name like '" + processName + 
                "%'\" get ProcessID,Commandline /format:list")
  end
  
  def self.queryJavaProcess
    return self.queryProcess('java')
  end  
  
  def to_s
    @pid.to_s + " " + @cmdLine
  end 
end 

puts ProcessInfo.queryProcess(ARGV[0])

Thursday, October 06, 2011

Equivalent of ps -ef | grep java in Windows

If you want to list a process and its command line, process id in Windows, this is the command:

 wmic process where "name='java.exe'" get ProcessID, Commandline /format:list

That is the equivalent of "ps -ef | grep java" in Linux. Then you could use taskkill /f /pid <PID> to kill it.

Friday, September 23, 2011

Install JDK on Windows without a UI

in CMD.exe shell:

./jdk-6u27-windows-x64.exe /s INSTALLDIR=\c:\jdk\jdk1.6.0_27-x64

This will allow you to install the JDK automatically without any GUI prompt, ideal for automation script.Note the forward slash before the c: drive is necessary.

Thursday, April 07, 2011

Force JBoss to load your lib jars

If you're running JBoss and you want it to favor librarires that go with your WAR instead of using what JBoss bundled with. Add this jboss-web.xml to your WEB-INF dir


<jboss-web>
<class-loading java2classloadingcompliance="false">
<loader-repository>
org.terracotta:archive=all-in-one.war
<loader-repository-config>
java2ParentDelegation=false
</loader-repository-config>
</loader-repository>
</class-loading>
</jboss-web>

Substitute "org.terracotta" for your package name and "all-in-one.war" for your WAR