Classpath variables can be used in a Java Build Path to avoid a reference to the local file system. Using a variable entry, the classpath only contains a variable and the build path can be shared in a team. The value of such variables is configured on the  Java > Build Path > Classpath Variables preference page.

 

 

众所周知,eclipse的project bulid path中可以引用第三方类库(如图1),

图1

但是这种方式有个缺点:对类库的引用是通过绝对路径。如果有两台电脑(办公室1台、家1台),很可能造成因为类库的路径不同导致eclipse报错。

但事实上,两台电脑中只是libraries目录的绝对路径不同,从libraries开始的子目录完全同步。

只要满足这个条件,就可以用下面的方式引入类库:

1.建立classpath variable。在window->preferences中的建立新的classpath variable(如图2);

图2

2.在build path中选择add varibale,然后在弹出的窗口中选择变量项并选择extend...(如图3)

图3

3.在variable extend窗口中选择所需要的类库(如图4)

图4

这样,类库就是以基于classpath variable的相对路径引用(如图5),

图5

只要能保证两台电脑上所有类库从某一目录开始的相对路径一致,就可以通过建立eclipse的classpath variable保证项目在两台电脑迁移的过程中不会因为引用类库而报错。