最近发现在 Eclipse 中进行远程调试别的主机上的应用的时候,总是连接不上调试端口,报 “Failed to connect to remote vm”, 如下图所示:

因为以前都是可以的,所以百思不得其解,Google了半天,能找到的问题一般也都是因为对方没有开启远程调试端口,所以自然连不上,但是我这里显然不是这个情况。在困惑了好久以后,突然想到会不会是受到了 Eclipse 代理设置的影响?虽然觉得不太可能,因为我的理解是那个代理只是给 Eclipse 本身用的,并不是给通过 Eclipse 启动的应用程序用的。但是结果证明是这个原因:
代理设置之前:

去掉代理之后,远程调试就可以了。

也许有人会觉得无聊,好好的 Eclipse 为什么要设置个代理呢。
没办法啊,两个原因,一个是公司的网络出去的话会有点困难的;另外一个就是有些Eclipse插件被墙掉了,比如Maven,所以不得已,只能设置代理来更新插件了。
最近项目需要写段代码来做版本的比较,需求大致是:
版本号一般是四位,如 1.0.0.10p7(也可能会少几位),要求比较版本的 先后/大小 关系从而进行一些处理。
刚开始觉得是一个非常常用的功能,应该在很多地方都有现成实现的。但是找了一些开源的项目以后,都没有找到合适的。
像 Maven 里面是也有关于版本判断的代码,但是过于复杂,有好多个类。而我是想最好能有一个函数,把两个版本传进去,告诉我比较的结果就好了。
找不到,于是自己写了个,能满足我的要求了。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
| private static final Pattern PATTERN_DIGI_VERSION = Pattern.compile("([\\d\\.]+)([abp]\\d+)?");
...
/**
* Compare two version strings, such as 1.0.0.10p7 and 1.0.0.20
*
* @param lv, the left version string to be compared
* @param rv, the right version string to be compared
* @return, 1 if lv>rv; 0 if lv==rv; -1 if lv<rv
*/
public static int compareVersion( String lv, String rv ) {
String[] lvs = lv.split("\\.");
String[] rvs = rv.split("\\.");
int lmin = Math.min(lvs.length, rvs.length);
for ( int i=0; i<lmin; i++ ) {
Matcher lmatch = PATTERN_DIGI_VERSION.matcher(lvs[i]);
Matcher rmatch = PATTERN_DIGI_VERSION.matcher(rvs[i]);
if ( lmatch.matches() ) {
int lv1 = Integer.valueOf(lmatch.group(1));
String strlv2 = lmatch.group(2);
if ( rmatch.matches() ) {
int rv1 = Integer.valueOf(rmatch.group(1));
String strrv2 = rmatch.group(2);
if ( lv1 > rv1 ) return 1;
else if ( lv1 < rv1 ) return -1;
else {
// compare to minor version, such a, b, p1, p3, etc.
if ( strlv2 == null || strlv2.length()==0 ) {
if ( strrv2 != null && strrv2.length() !=0 ) return -1;
} else {
if ( strrv2 == null || strrv2.length() == 0 ) return 1;
else {
String wlv = strlv2.substring(0, 1);
int lv2 = strlv2 == null ? -1 : Integer.valueOf(strlv2.substring(1));
String wrv = strrv2.substring(0, 1);
int rv2 = strrv2 == null ? -1 : Integer.valueOf(strrv2.substring(1));
// compare small version string first, such as 'a', 'b', 'p'
if ( wlv.compareTo(wrv) > 0 ) return 1;
else if ( wlv.compareTo(wrv) < 0 ) return -1;
else {
if ( lv2 > rv2 ) return 1;
else if ( lv2 < rv2 ) return -1;
}
}
}
}
} else {
return 1;
}
} else {
return -1;
}
}
if ( lmin < lvs.length ) return 1;
else if ( lmin == lvs.length && lmin == rvs.length ) return 0;
else return -1;
} |
跟 C 语言不同,Java 中没有 unsigned 类型,这点可能让很多人都有过困扰。
那如果碰到确实需要用到 unsigned 类型的情况怎么办呢? 通常的办法就是用更大一号的类型来处理,比如:
- 处理 unsigned byte,就用 int;
- 处理 unsigned int,就用 long;
- 处理 unsigned long,这个不好办,只能用 BigInteger 了。但是要注意的是,Java 中的 long 是 8 bytes,而 C 语言只有4 bytes,所以碰到这种情况的话,那就等同于上面 unsigned int 的情况了。
另外,Java 中比 C 语言增加了一种不带符号的移位操作: >>> ,固定的用 0 来填充左边高位的空缺,这在一定程度上减轻了没有 unsigned 类型带来的影响。
扩展阅读:这里有篇文章介绍了 Java 中怎么处理 unsigned 类型的。文中提到的一篇对 C /C++/Java 语言创始人的访谈中提到了 Java 中没有定义 unsigned 类型的原因,见最后 Gosling 的部分:
Q: Programmers often talk about the advantages and disadvantages of programming
in a "simple language." What does that phrase mean to you, and is [C/C++/Java] a
simple language in your view?
Ritchie: C (and the others for that matter) are simple in some ways, though they are
also subtle; other, somewhat similar languages like Pascal are arguably simpler. What
has become clear is that aspects of the environment like libraries that aren't part of
the core language are much bigger and more complicated. The 1999 C standard grew
hugely more in the library part than in the language; the C++ STL and other things
are big; AWT and other things associated with Java are too.
Stroustrup: I see three obvious notions of "simple:" to be easy to learn, to make it
easy to express ideas, and to have a one-to-one correspondence to some form of
math. In those terms, none of the three languages is simple. However, once mastered,
C and C++ make it easy to express quite complex and advanced ideas -- especially
when those ideas have to be expressed under real-world resource constraints.
Gosling: For me as a language designer, which I don't really count myself as these days,
what "simple" really ended up meaning was could I expect J. Random Developer to
hold the spec in his head. That definition says that, for instance, Java isn't -- and
in fact a lot of these languages end up with a lot of corner cases, things that nobody
really understands. Quiz any C developer about unsigned, and pretty soon you discover
that almost no C developers actually understand what goes on with unsigned,
what unsigned arithmetic is. Things like that made C complex.
The language part of Java is, I think, pretty simple. The libraries you have to look up.
通过 LocateRegistry.createRegistry(port) 创建了一个 rmiregistry 监听 port 所在端口,
如果想要在程序中把这个端口关闭,也就是关闭 RMI registry 服务,可以参考以下的代码。
注:似乎在2003年的时候,这个方法还行不通,到了 JDK6 的版本才解决这个问题的。
参考 Sun bug 4457683, bug 4508962, 以及 这个 mail archive。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
| import java.rmi.NoSuchObjectException;
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
public class RMITest {
public void testRMIPortRelease() {
System.out.println("Testing port release");
System.out.println("Acquiring port... ");
Remote reg = null;
try {
reg = LocateRegistry.createRegistry(1102);
} catch (RemoteException ex) {
ex.printStackTrace();
}
System.out.println("releasing port... ");
try {
java.rmi.server.UnicastRemoteObject.unexportObject(reg, true);
} catch (NoSuchObjectException ex1) {
ex1.printStackTrace();
}
reg = null;
}
public static void main(String[] args) {
RMITest s = new RMITest();
s.testRMIPortRelease();
}
} |
原文出处: http://ventrix.nsdc.gr/code_folds/2008/10/07/java-prime-number/
Information about the author: http://kospol.nsdc.gr/
========================================================================
This is one approach to check if the given number is a prime number. It can take a very big number as an argument. It uses no threads and has no comments. Think it as version 0.01.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
| /*
* Prime.java
*
* Copyright 2008 Ventrix <ventrix@gmail.com>
*
* http://ventrix.nsdc.gr/code_folds/
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*/
import java.math.BigInteger;
public class Prime {
private static long start;
private static long end;
public static void main(String[] argv) {
boolean isprimen;
//http://java.sun.com/j2se/1.4.2/docs/api/java/math/BigInteger.html#isProbablePrime(int)
//http://primes.utm.edu/lists/small/small.html
//for i in `seq 1 1000`; do java Prime $i >> primes; done
//cat primes | grep "is a"
start = System.currentTimeMillis();
try {
BigInteger bigNumber = new BigInteger(argv[0]);
if (bigNumber.compareTo(new BigInteger("2147483647")) == 1) {
if (bigNumber.compareTo(new BigInteger("9223372036854775807")) == -1) {
Long longNumber = new Long(argv[0]);
bigNumber = null;
isprimen = isNorPrime(longNumber);
} else {
isprimen = isBigPrime(bigNumber);
}
} else {
bigNumber = null;
Integer intNumber = new Integer(argv[0]);
isprimen = isPrime(intNumber);
}
if (isprimen) {
System.out.println(argv[0] + " is a prime!");
} else {
System.out.println(argv[0] + " is NOT a prime!");
}
end = System.currentTimeMillis();
System.out.println("Completed in +" + (end - start) + "ms");
} catch (Exception e) {
e.printStackTrace();
}
}
private static boolean isBigPrime(BigInteger number) {
System.out.println("You gave me a BIG number");
BigInteger[] remain;
remain = number.divideAndRemainder(new BigInteger(new Integer("2").toString()));
if (remain[1].compareTo(new BigInteger("0")) == 0) {
return false;
}
remain = number.divideAndRemainder(new BigInteger(new Integer("3").toString()));
if (remain[1].compareTo(new BigInteger("0")) == 0) {
return false;
}
int y = 2;
int x = (int) Math.sqrt(number.doubleValue());
for (int i = 5; i <= x; i += y, y = 6 - y) {
remain = number.divideAndRemainder(new BigInteger(new Integer(i).toString()));
if (remain[1].compareTo(new BigInteger("0")) == 0) {
return false;
}
}
return true;
}
private static boolean isNorPrime(Long number) {
System.out.println("You gave me a normal number");
if (number % 2 == 0) {
return false;
}
if (number % 3 == 0) {
return false;
}
int y = 2;
int x = (int) Math.sqrt(number);
for (int i = 5; i <= x; i += y, y = 6 - y) {
if (number % i == 0) {
return false;
}
}
return true;
}
private static boolean isPrime(Integer number) {
System.out.println("You gave me a small number");
if (number < 2) {
return false;
}
if (number == 2) {
return true;
}
if (number % 2 == 0) {
return false;
}
if (number == 3) {
return true;
}
if (number % 3 == 0) {
return false;
}
int y = 2;
int x = (int) Math.sqrt(number);
for (int i = 5; i <= x; i += y, y = 6 - y) {
if (number % i == 0) {
return false;
}
}
return true;
}
}//class</ventrix@gmail.com> |