缘起:
已运行的一Java程序,由于某些原因,该程序的配置文件(.properties文件)被删除了。
现在需要恢复此配置文件,以便此程序可以多次重启。现在已经不敢重启程序了。
(已另外想办法欲从文件系统进行恢复,未果。)问题:
如何才能获取此程序内存中的信息,将配置信息抓出来呢?尝试过如下的方法,但由于程序运行在linux系统中,所以未成功。
http://www.blogjava.net/javacap/archive/2007/01/04/91735.html
http://bbs.chinaunix.net/thread-880524-1-1.html请问各位有何良策,不胜感激!

解决方案 »

  1.   

    attach api
    这个库不包含在jre的库中,在jdk的lib里tools.jar
      

  2.   

    谢谢!
    请问有什么工具推荐吗?系统是Red Hat Enterprise Linux 5.x
      

  3.   

    看看这个帖子:
    http://topic.csdn.net/t/20030922/21/2288646.html
      

  4.   

    代码注入?这个不好搞吧,JVM进程的中的数据结构很难解析吧
      

  5.   

    http://www.zeuux.org/blog/content/3430/package com.tools;import java.io.BufferedReader;
    import java.io.File;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.util.Arrays;
    import java.util.Set;import javax.management.AttributeNotFoundException;
    import javax.management.InstanceNotFoundException;
    import javax.management.IntrospectionException;
    import javax.management.MBeanAttributeInfo;
    import javax.management.MBeanException;
    import javax.management.MBeanInfo;
    import javax.management.MBeanOperationInfo;
    import javax.management.MBeanServerConnection;
    import javax.management.MalformedObjectNameException;
    import javax.management.ObjectInstance;
    import javax.management.ObjectName;
    import javax.management.ReflectionException;
    import javax.management.remote.JMXConnector;
    import javax.management.remote.JMXConnectorFactory;
    import javax.management.remote.JMXServiceURL;import sun.management.ConnectorAddressLink;import com.sun.tools.attach.AgentInitializationException;
    import com.sun.tools.attach.AgentLoadException;
    import com.sun.tools.attach.AttachNotSupportedException;
    import com.sun.tools.attach.VirtualMachine;
    public class AttachDemo
    {
        public void test(String pid) throws NumberFormatException, IOException, AttachNotSupportedException, AgentLoadException, AgentInitializationException, MalformedObjectNameException, NullPointerException, InstanceNotFoundException, IntrospectionException, ReflectionException, AttributeNotFoundException, MBeanException
        {    
            String domainName = "Erosa";
        
            String localJmxAddress = ConnectorAddressLink.importFrom(Integer.valueOf(pid).intValue());
            if (localJmxAddress == null) 
            {
              VirtualMachine vm = VirtualMachine.attach(pid);
              String connectorAddress = vm.getAgentProperties().getProperty("com.sun.management.jmxremote.localConnectorAddress");
              if (connectorAddress == null) 
              {
                String agent = vm.getSystemProperties().getProperty("java.home") + File.separator + "lib" + File.separator + "management-agent.jar";
                vm.loadAgent(agent);
                connectorAddress = vm.getAgentProperties().getProperty("com.sun.management.jmxremote.localConnectorAddress");
              }
              if (connectorAddress == null) 
              {
                System.err.println("Failed to get Local JMX Address by Pid. Exit Without Further Processing.");
                System.exit(-1);
              }
              localJmxAddress = connectorAddress;
            }
        
            BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
            JMXConnector connector = JMXConnectorFactory.newJMXConnector(new JMXServiceURL(localJmxAddress), null);
            try 
            {
              connector.connect();
              MBeanServerConnection connection = connector.getMBeanServerConnection();
              Set<ObjectInstance> set = connection.queryMBeans(new ObjectName(domainName + ":*"), null);
              for (ObjectInstance ins : set) 
              {
                System.out.println("==================================================================");
        
                ObjectName name = ins.getObjectName();
        
                System.out.println("MBean : " + name.toString());
        
                MBeanInfo beaninfo = connection.getMBeanInfo(name);
                MBeanAttributeInfo[] attributes = beaninfo.getAttributes();
                System.out.println("-----------Attributes------------");
                for (MBeanAttributeInfo attr : attributes) 
                {
                  System.out.println(attr.getName() + ": " + connection.getAttribute(name, attr.getName()));
                }
                MBeanOperationInfo[] operations = beaninfo.getOperations();
                System.out.println("-----------Operations------------");
                for (MBeanOperationInfo operation : operations) 
                {
                  System.out.println("operation: " + operation.getName() + " " + Arrays.toString(operation.getSignature()));
                  if (operation.getName().startsWith("dump")) 
                  {
                    System.out.println(connection.invoke(name, operation.getName(), new Object[0], new String[0]));
                  }
                }
              }
              System.out.println("exit jmx client.");
            }
            finally
            {
              connector.close();
              reader.close();
            }
      }
    }
      

  6.   

    楼主可以从 那套 解析.properties文件并封装其数据的类下手,访问这些类,把访问到的数据重新写入一个properties文件...