插件安装
安装 Java Extension Pack 插件,会自动安装以下四个插件:
- Language Support for Java(TM) by Red Hat
- Debugger for Java
- Java Test Runner
- Maven for Java
在vscode中可以通过以上插件配置java编程环境。但是如果不用maven、gradle等项目构建工具,后两个插件是无法使用的,而且对于简单的仅有.java文件的项目结构,就我找遍各种资料,也没有找到导入jar包的方法,自动识别文件内容有时也会出错。配置jdk路径
打开settings.json,在用户设置中添加"java.home": "D:\\Program Files\\Java\\jdk-11.0.1"
,run and debug
- 找不到依赖文件
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
91public class NBody{
/** return the radius of the universe in file */
public static double readRadius(String filename){
double radius=0;
try{
In in = new In(filename);
in.readInt();
radius = in.readDouble();
}
catch(Exception e){
System.out.println(e);
}
// System.out.println("Read file " + filename + " successful!");
return radius;
}
/** return an array of planets from file */
public static Planet[] readPlanets(String filename){
Planet[] planets = null;
try{
In in = new In(filename);
int N = in.readInt();
in.readDouble();
/** construct planets array */
planets = new Planet[N];
for(int i=0;i<N;i++){
double xP = in.readDouble();
double yP = in.readDouble();
double xV = in.readDouble();
double yV = in.readDouble();
double m = in.readDouble();
String img = in.readString();
planets[i] = new Planet(xP, yP, xV, yV, m, img);
}
System.out.println("Read file " + filename + " successful!");
}
catch(Exception e){System.out.println(e);}
System.out.println();
return planets;
}
public static void main(String[] args) {
double T = Double.parseDouble(args[0]);
double dt = Double.parseDouble(args[1]);
String filename = args[2];
double radius = readRadius(filename);
Planet[] planets = readPlanets(filename);
StdAudio.play("./audio/2001.mid");
StdDraw.setScale(-radius,radius);
StdDraw.enableDoubleBuffering();
double time = 0;
while(time < T){
double[] xForces = new double[planets.length];
double[] yForces = new double[planets.length];
/** save the forces to xForces and yFroces arrays */
for(int i=0;i<planets.length;i++){
xForces[i] = planets[i].calcNetForceExertedByX(planets);
yForces[i] = planets[i].calcNetForceExertedByY(planets);
}
/** update all planets */
for(int i=0;i<planets.length;i++){
planets[i].update(dt, xForces[i], yForces[i]);
}
StdDraw.clear();
/** draw image background */
StdDraw.picture(0, 0, "./images/starfield.jpg");
/** draw all the planets */
for(Planet p : planets){
p.draw();
}
StdDraw.show();
StdDraw.pause(10);
time += dt;
}
StdOut.printf("%d\n", planets.length);
StdOut.printf("%.2e\n", radius);
for (int i = 0; i < planets.length; i++) {
StdOut.printf("%11.4e %11.4e %11.4e %11.4e %11.4e %12s\n",
planets[i].xxPos, planets[i].yyPos, planets[i].xxVel,
planets[i].yyVel, planets[i].mass, planets[i].imgFileName);
}
}
}
上面的java程序依赖In, Planet, StdAudio,StdDraw,StdOut
而进行Run/Debug时会报错,找不到In,StdAudio
等,但是把对应java文件双击打开后,运行正常。
- 需要命令行参数,则配置launch.json中的”args”
1
2
3
4
5
6
7
8
9
10
11{
"configurations": [
{
"type": "java",
"name": "CodeLens (Launch) - NBody",
"request": "launch",
"mainClass": "NBody",
"args": "157788000.0 25000.0 data/planets.txt"
}
]
}
library path
不用maven什么的话,现在没有办法加载库,通过calssPath也不行。