intellij idea安装教程和项目创建和运行
安装教程:
https://blog.csdn.net/2401_84239901/article/details/137701540
IDEA下载地址:https://www.jetbrains.com//idea/download/#section=windows
IntelliJ IDEA新建maven项目
https://blog.csdn.net/m0_57532432/article/details/135991277
点击运行之后,多出来target目录
右击运行
也就是都可以运行,这两处
写了个Java文件,里面用package的时候,指定是目录名,从项目根目录下src/main/java下开始写路径。运行代码的时候,生成target目录下的clsses,根据上面写的代码路径生成对应的class文件
新建一个spingboot项目
package com.example.demo; /** * Hello Controller */ @RestController public class HelloController { @GetMapping("/") public String hello() { return "Hello World" } }
package com.example.demo; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; /** * Hello Controller */ @RestController public class HelloController { @GetMapping("/") public String hello() { return "Hello World"; } }
导入之后还是有报错
OpenJDK 64-Bit Server VM warning: Sharing is only supported for boot loader classes because bootstrap classpath has been appended
上面新建项目,指定的是jar,现在指定war试试:
然后点击运行
可以访问到:
新增一个路由
package com.example.demo; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; /** * Hello Controller */ @RestController public class HelloController { @GetMapping("/") public String hello() { return "Hello World"; } @GetMapping("/mcw") public String hello2() { return "Hello machangwei"; } }
重启项目,可以正常访问到
idea中把spring boot项目打成jar包
打jar包
打开项目,右击项目选中Open Module Settings进入project Structure
选中Artifacts,点击中间的加号(Project Settings->Artifacts->JAR->From modules with dependencies )
弹出Create JAR from Modules,选择‘Main Class’,然后一路ok
点击右侧的Maven Projects,打开LIfecycle,先点击clean,再点击package,生成target文件夹(在该项目文件夹内),里面有以项目名命名加版本号的jar文件,打包完成啦
运行jar包
入jar所在的文件夹,使用java -jar命令运行jar,项目就能启动
java -jar jar包名-SNAPSHOT.jar
@@@
清理,是删除之前的target目录
C:\Users\89723\.jdks\corretto-17.0.11\bin\java.exe -Dmaven.multiModuleProjectDirectory=D:\softwareinstall\ideaIU-2024.1.4.win\myproject\demo1 -Djansi.passthrough=true -Dmaven.home=C:\Users\89723\.m2\wrapper\dists\apache-maven-3.9.7-bin\3k9n615lchs6mp84v355m633uo\apache-maven-3.9.7 -Dclassworlds.conf=C:\Users\89723\.m2\wrapper\dists\apache-maven-3.9.7-bin\3k9n615lchs6mp84v355m633uo\apache-maven-3.9.7\bin\m2.conf -Dmaven.ext.class.path=D:\softwareinstall\ideaIU-2024.1.4.win\plugins\maven\lib\maven-event-listener.jar -javaagent:D:\softwareinstall\ideaIU-2024.1.4.win\lib\idea_rt.jar=64788:D:\softwareinstall\ideaIU-2024.1.4.win\bin -Dfile.encoding=UTF-8 -classpath C:\Users\89723\.m2\wrapper\dists\apache-maven-3.9.7-bin\3k9n615lchs6mp84v355m633uo\apache-maven-3.9.7\boot\plexus-classworlds-2.8.0.jar;C:\Users\89723\.m2\wrapper\dists\apache-maven-3.9.7-bin\3k9n615lchs6mp84v355m633uo\apache-maven-3.9.7\boot\plexus-classworlds.license org.codehaus.classworlds.Launcher -Didea.version=2024.1.4 clean
[INFO] Scanning for projects...
[INFO]
[INFO] -------------------------< org.example:demo1 >--------------------------
[INFO] Building demo1 0.0.1-SNAPSHOT
[INFO] from pom.xml
[INFO] --------------------------------[ war ]---------------------------------
[INFO]
[INFO] --- clean:3.3.2:clean (default-clean) @ demo1 ---
[INFO] Deleting D:\softwareinstall\ideaIU-2024.1.4.win\myproject\demo1\target
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.355 s
[INFO] Finished at: 2024-07-07T14:35:32+08:00
[INFO] ------------------------------------------------------------------------
Process finished with exit code 0
package是生成新的打包目录
.......
Downloaded from central: https://repo.maven.apache.org/maven2/org/junit/platform/junit-platform-commons/1.9.3/junit-platform-commons-1.9.3.jar (103 kB at 373 kB/s)
Downloaded from central: https://repo.maven.apache.org/maven2/org/junit/platform/junit-platform-engine/1.9.3/junit-platform-engine-1.9.3.jar (189 kB at 588 kB/s)
[INFO]
[INFO] -------------------------------------------------------
[INFO] T E S T S
[INFO] -------------------------------------------------------
[INFO] Running com.example.demo.Demo1ApplicationTests
14:36:29.018 [main] INFO org.springframework.test.context.support.AnnotationConfigContextLoaderUtils -- Could not detect default configuration classes for test class [com.example.demo.Demo1ApplicationTests]: Demo1ApplicationTests does not declare any static, non-private, non-final, nested classes annotated with @Configuration.
14:36:29.095 [main] INFO org.springframework.boot.test.context.SpringBootTestContextBootstrapper -- Found @SpringBootConfiguration com.example.demo.Demo1Application for test class com.example.demo.Demo1ApplicationTests
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v3.3.1)
2024-07-07T14:36:29.382+08:00 INFO 14716 --- [demo1] [ main] com.example.demo.Demo1ApplicationTests : Starting Demo1ApplicationTests using Java 17.0.11 with PID 14716 (started by 89723 in D:\softwareinstall\ideaIU-2024.1.4.win\myproject\demo1)
2024-07-07T14:36:29.383+08:00 INFO 14716 --- [demo1] [ main] com.example.demo.Demo1ApplicationTests : No active profile set, falling back to 1 default profile: "default"
2024-07-07T14:36:30.193+08:00 INFO 14716 --- [demo1] [ main] com.example.demo.Demo1ApplicationTests : Started Demo1ApplicationTests in 0.988 seconds (process running for 1.63)
OpenJDK 64-Bit Server VM warning: Sharing is only supported for boot loader classes because bootstrap classpath has been appended
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 1.898 s -- in com.example.demo.Demo1ApplicationTests
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO]
[INFO] --- war:3.4.0:war (default-war) @ demo1 ---
[INFO] Packaging webapp
[INFO] Assembling webapp [demo1] in [D:\softwareinstall\ideaIU-2024.1.4.win\myproject\demo1\target\demo1-0.0.1-SNAPSHOT]
[INFO] Processing war project
[INFO] Building war: D:\softwareinstall\ideaIU-2024.1.4.win\myproject\demo1\target\demo1-0.0.1-SNAPSHOT.war
[INFO]
[INFO] --- spring-boot:3.3.1:repackage (repackage) @ demo1 ---
[INFO] Replacing main artifact D:\softwareinstall\ideaIU-2024.1.4.win\myproject\demo1\target\demo1-0.0.1-SNAPSHOT.war with repackaged archive, adding nested dependencies in BOOT-INF/.
[INFO] The original artifact has been renamed to D:\softwareinstall\ideaIU-2024.1.4.win\myproject\demo1\target\demo1-0.0.1-SNAPSHOT.war.original
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 6.976 s
[INFO] Finished at: 2024-07-07T14:36:32+08:00
[INFO] ------------------------------------------------------------------------
Process finished with exit code 0
从结果看貌似是war包,再来一次,也是这样
运行这个包,
D:\>C:\Users\89723\.jdks\corretto-17.0.11\bin\java -jar D:\softwareinstall\ideaIU-2024.1.4.win\myproject\demo1\target\demo1-0.0.1-SNAPSHOT.war
成功用8080端口开启服务
我们在此之前已经将编辑器上的关闭了的
可以看的,jar包启动,正常提供服务,哪个包名war结尾,不知道是啥情况