项目中有时需要在Java类中启动一个长时间运行的服务进程(监听进程、监控进程等),且该进程需要在tomcat启动时启动,在tomcat停止时退出。在项目开发的过程中,我需要在Java类中启动一个监听进程,而在该进程成功启动后,一调用该监听服务进程时,该进程就挂掉(退出),现将解决方法记录如下。

Continue reading

本文记录的是如何在Java类中调用外部可执行脚本,例如shell脚本、Python脚本、ruby脚本等。文中阐述了Runtime.exec和ProcessBuilder.start两种调用脚本的方式,官方推荐使用后者的方式,下面对其分别讲述。

Continue reading

本文记录的是如何使用markdown命名锚(names anchors),当使用命名锚时,我们可以创建文章目录,直接跳至该命名锚(比如页面中某个小节)的链接,这样使用者就无需不停地滚动页面来寻找他们需要的信息了。

其实使用markdown不能直接达到定义命名锚的目的,借助了html的功能。在定义标题时不使用markdown语法(#个数对应相应级别的HTML样式标题),而使用h1/h2等,且使用id属性命名锚,如:

Continue reading

本文记录的是shell之printf格式化输出.

首先看看其 man page 内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
NAME
printf -- formatted output

SYNOPSIS
printf format [arguments ...]

DESCRIPTION
The printf utility formats and prints its arguments, after
the first, under control of the format. The format is a character string which contains three types of objects: plain characters,
which are simply copied to standard output, character escape
sequences which are converted and copied to the standard
output, and format specifications, each of which causes printing
of the next successive argument.

The arguments after the first are treated as strings if the
corresponding format is either c, b or s;otherwise it is evaluated
as a C constant, with the following extensions:

o A leading plus or minus sign is allowed.
o If the leading character is a single or double quote, the value is the ASCII code of the next character.

从上述 man page 内容可以看出:

  1. printf通过format字符串格式化输出其后的arguments
  2. format字符串包含三种类型对象: 普通字符串,该对象输出时会直接拷贝到标准输出STDOUT;字符转义序列,通过字符转义之后输出到标准输出;格式说明符,每一个格式说明符对应输出相应的argument
  3. 如果对应的格式指示符是%c/%b/%s时,相对应的参数都视为字符串,否则它们会被解释为C语言的数字常量: 在其开头可使用正负号标识;如果字符串开头是单引号'或者双引号",那么打印输出的值是紧跟着单引号或者双引号后的那个字符的ASCII
Continue reading

本文记录的是关于shell之echo的知识。

echo是linux中常用的终端命令(command line),其包括:

1
command_name option argument

首先看看echo的 man page:

1
2
3
4
5
6
7
8
9
NAME
echo -- write arguments to the standard output

SYNOPSIS
echo [-n] [string ...]

DESCRIPTION
The echo utility writes any specified operands, separated by single blank (' ') characters and followed
by a newline (`\n') character, to the standard output.

从帮助文档可以看出:

  1. echo是将其后的arguments输出到(STDOUT),通常就是bash界面.
  2. echo是将所有的操作对象,以单一的空格` `分开的字符串,最后紧跟着换行符(''),输出到bash
Continue reading

本文记录的是如何使用Gson序列化和反序列化包含不同类型对象的 JSON Array,例如:

1
['hello',5,{name:'GREETINGS',source:'guest'}]

对于这种情况看,使用 Gson toJson(collection) 序列化,无需做额外的工作都能够得到正确的结果,但是,使用 fromJson(json, Collection.class) 反序列化时不能得到期望结果,因为Gson不知道如何将集合中元素与其类型对应起来。可采用如下解决方法: 使用Gson parser API(JsonParser)解析数组中每个元素,然后对数组中每个元素使用Gson.fromJson()进行反序列化。示例如下:

Continue reading

本文记录的是使用Gson如何序列化和反序列化实体中部分属性,有两种方式,使用transient和使用注解@Expose.

默认情况下,如果使用transient标注属性,那个该属性在序列化和反序列化时就会被屏蔽。同样地,如果属性声明为static类型,那么该属性也会被屏蔽. 如下示例所示:

Student对象:

1
2
3
4
5
6
7
public class Student {
private int stuNo;
private transient int sex;
private String name;

//......
}
Continue reading

本文记录的是如何使用Gson处理List对象数据, 包括序列化List对象以及将Json串反序列化为List对象.

首先定义一个Student类, 其属性都是基本类型, 如下所示:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public class Student {
private int stuNo;
private int sex;
private String name;
public Student() {
}

public Student(int stuNo, int sex, String name) {
this.stuNo = stuNo;
this.sex = sex;
this.name = name;
}
//getter & setter
......
}
Continue reading

本文记录的是如何使用Gson处理Array类型数据, 包括将Array对象序列化为Json串以及将Json串反序列化为Array对象.

下面的示例是将数组元素序列化为Json串,如下所示:

Serialization:

1
2
3
4
5
Gson gson = new Gson();
int[] ints = {1, 2, 3, 4, 5};
String[] strings = {"abc", "def", "ghi"};
System.out.println("json_string: " + gson.toJson(ints));
System.out.println("json_string: " + gson.toJson(strings));
Continue reading

本文记录的是在进行Net-SNMP配置及测试时经常用到的命令,具体的使用方法可参考命令的帮助文档。

####1. net-snmp-create-v3-user#### 正如命令的字面意思,该命令用来创建 Net-SNMP v3用户。

####2. net-snmp-config####

1
2
3
4
5
6
7
The net-snmp-config shell script is designed to retrieve the  
configuration information about the libraries and binaries dealing
with the Simple Network Management Protocol (SNMP), built from the
net-snmp source package. The information is particularily useful
for applications that need to link against the SNMP libraries and
hence must know about any other libraries that must be linked in
as well.

net-snmp-config命令用来获取处理SNMP协议的库和二进制文件的配置信息,其从Net-SNMP的源文件包编译而得。该信息对那些依赖于SNMP库的应用尤其有用.

Continue reading
Author's picture

CaryaLiu

@Chengdu


iOS Developer


Chengdu