JDK及IDE在Mac OS系统中的安装
- JDK 下载与安装
- JDK 配置
- Intellij IDEA安装
- Intellij IDEA 配置JDK
JDK 下载与安装
这个简单,几乎不会出问题,我用的Oracle14,官网下载,如果觉得官网太慢了,也可以通过百度网盘 下载,密码: tabt,安装过程没有任何困难,照着指示走就行。
通过指令
java -version
检查java安装是否成功,成功Terminal会输出
java version "14.0.1" 2020-04-14
Java(TM) SE Runtime Environment (build 14.0.1+7)
Java HotSpot(TM) 64-Bit Server VM (build 14.0.1+7, mixed mode, sharing)
或者通过输入指令
/usr/libexec/java_home -V
检查Java Virtual Machine的数量,路径以及当前使用Java路径
Matching Java Virtual Machines (1):#数量14.0.1, x86_64: "Java SE 14.0.1" /Library/Java/JavaVirtualMachines/jdk-14.0.1.jdk/Contents/Home#Java版本及路径/Library/Java/JavaVirtualMachines/jdk-14.0.1.jdk/Contents/Home#当前运行Java版本路径
JDK 配置
回到根目录
cd ~/
如果第一次配置环境变量,多出一步,输入如下指令
touch .bash_profile
然后,打开它
open -e .bash_profile
输入下面几行代码
JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.14.1.jdk/Contents/Home
PATH=$JAVA_HOME/bin:$PATH:.
CLASSPATH=$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar:.
export JAVA_HOME
export PATH
export CLASSPATH
其中,JAVA_HOME
指代Java的根路径(JDK的安装路径),此变量在后续运行Intellij IDEA时会被需要;path
让Java命令被系统可以在任何路径下识别,path本就存在不用新建,而且是编辑添加而不是覆盖,否则其他有些指令会找不到;classpath
是加载类(class)的路径,用于加载时Java对其的识别。保存关闭窗口。
特别注意,版本不同,这些Path会有稍许不同。
P.S. 也可以通过
vim ~/.bash_profile
指令去添加路径
终端输入用于激活bash:
source .bash_profile
最后检验Java是否配置成功:
java -version
或者
echo $JAVA_HOME
会返回之前输入的路径
/Library/Java/JavaVirtualMachines/jdk1.14.0.1.jdk/Contents/Home
对于安装成功的版本,输入java
, Terminal会输出
Usage: java [options] <mainclass> [args...](to execute a class)or java [options] -jar <jarfile> [args...](to execute a jar file)or java [options] -m <module>[/<mainclass>] [args...]java [options] --module <module>[/<mainclass>] [args...](to execute the main class in a module)or java [options] <sourcefile> [args](to execute a single source-file program)Arguments following the main class, source file, -jar <jarfile>,-m or --module <module>/<mainclass> are passed as the arguments tomain class.where options include:-cp <class search path of directories and zip/jar files>-classpath <class search path of directories and zip/jar files>--class-path <class search path of directories and zip/jar files>A : separated list of directories, JAR archives,and ZIP archives to search for class files.-p <module path>--module-path <module path>...A : separated list of directories, each directoryis a directory of modules.--upgrade-module-path <module path>...A : separated list of directories, each directoryis a directory of modules that replace upgradeablemodules in the runtime image--add-modules <module name>[,<module name>...]root modules to resolve in addition to the initial module.<module name> can also be ALL-DEFAULT, ALL-SYSTEM,ALL-MODULE-PATH.--list-moduleslist observable modules and exit-d <module name>--describe-module <module name>describe a module and exit--dry-run create VM and load main class but do not execute main method.The --dry-run option may be useful for validating thecommand-line options such as the module system configuration.--validate-modulesvalidate all modules and exitThe --validate-modules option may be useful for findingconflicts and other errors with modules on the module path.-D<name>=<value>set a system property-verbose:[class|module|gc|jni]enable verbose output for the given subsystem-version print product version to the error stream and exit--version print product version to the output stream and exit-showversion print product version to the error stream and continue--show-versionprint product version to the output stream and continue--show-module-resolutionshow module resolution output during startup-? -h -helpprint this help message to the error stream--help print this help message to the output stream-X print help on extra options to the error stream--help-extra print help on extra options to the output stream-ea[:<packagename>...|:<classname>]-enableassertions[:<packagename>...|:<classname>]enable assertions with specified granularity-da[:<packagename>...|:<classname>]-disableassertions[:<packagename>...|:<classname>]disable assertions with specified granularity-esa | -enablesystemassertionsenable system assertions-dsa | -disablesystemassertionsdisable system assertions-agentlib:<libname>[=<options>]load native agent library <libname>, e.g. -agentlib:jdwpsee also -agentlib:jdwp=help-agentpath:<pathname>[=<options>]load native agent library by full pathname-javaagent:<jarpath>[=<options>]load Java programming language agent, see java.lang.instrument-splash:<imagepath>show splash screen with specified imageHiDPI scaled images are automatically supported and usedif available. The unscaled image filename, e.g. image.ext,should always be passed as the argument to the -splash option.The most appropriate scaled image provided will be picked upautomatically.See the SplashScreen API documentation for more information@argument filesone or more argument files containing options-disable-@filesprevent further argument file expansion--enable-previewallow classes to depend on preview features of this release
To specify an argument for a long option, you can use --<name>=<value> or
--<name> <value>.
输入javac
,输出端会输出:
Usage: javac <options> <source files>
where possible options include:@<filename> Read options and filenames from file-Akey[=value] Options to pass to annotation processors--add-modules <module>(,<module>)*Root modules to resolve in addition to the initial modules, or all moduleson the module path if <module> is ALL-MODULE-PATH.--boot-class-path <path>, -bootclasspath <path>Override location of bootstrap class files--class-path <path>, -classpath <path>, -cp <path>Specify where to find user class files and annotation processors-d <directory> Specify where to place generated class files-deprecationOutput source locations where deprecated APIs are used--enable-previewEnable preview language features. To be used in conjunction with either -source or --release.-encoding <encoding> Specify character encoding used by source files-endorseddirs <dirs> Override location of endorsed standards path-extdirs <dirs> Override location of installed extensions-g Generate all debugging info-g:{lines,vars,source} Generate only some debugging info-g:none Generate no debugging info-h <directory>Specify where to place generated native header files--help, -help, -? Print this help message--help-extra, -X Print help on extra options-implicit:{none,class}Specify whether or not to generate class files for implicitly referenced files-J<flag> Pass <flag> directly to the runtime system--limit-modules <module>(,<module>)*Limit the universe of observable modules--module <module>(,<module>)*, -m <module>(,<module>)*Compile only the specified module(s), check timestamps--module-path <path>, -p <path>Specify where to find application modules--module-source-path <module-source-path>Specify where to find input source files for multiple modules--module-version <version>Specify version of modules that are being compiled-nowarn Generate no warnings-parametersGenerate metadata for reflection on method parameters-proc:{none,only}Control whether annotation processing and/or compilation is done.-processor <class1>[,<class2>,<class3>...]Names of the annotation processors to run; bypasses default discovery process--processor-module-path <path>Specify a module path where to find annotation processors--processor-path <path>, -processorpath <path>Specify where to find annotation processors-profile <profile>Check that API used is available in the specified profile--release <release>Compile for the specified Java SE release. Supported releases: 7, 8, 9, 10, 11, 12, 13, 14-s <directory> Specify where to place generated source files--source <release>, -source <release>Provide source compatibility with the specified Java SE release. Supported releases: 7, 8, 9, 10, 11, 12, 13, 14--source-path <path>, -sourcepath <path>Specify where to find input source files--system <jdk>|none Override location of system modules--target <release>, -target <release>Generate class files suitable for the specified Java SE release. Supported releases: 7, 8, 9, 10, 11, 12, 13, 14--upgrade-module-path <path>Override location of upgradeable modules-verbose Output messages about what the compiler is doing--version, -version Version information-Werror Terminate compilation if warnings occur
Intellij IDEA安装
推荐官网直接下载,community版本是免费开源版本,但是相对功能没有ultimate齐全,在官网下载JetBrains的软件真的有点难受,网站经常登陆不上去,我把2020.2.3 ultimate 版本的下载放在了我的百度网盘,密码是df9w。同样安装按着教程走就对了,千万别用破解版,作为学生,用edu邮箱账户可以免费获得该版本的license,破解版会修改你电脑的hosts,而JB公司对很多破解版本的应对措施越来越有效,很可能你破解了以后会导致正常版本无法使用,而破解版本在几天后也失效。
Intellij IDEA 配置JDK
如上图点击左上角File>Project Structure
如上图,点击Platform Settings > SDKs > 中间 +号 Add SDK > JDK home path与之前JDK 时设置的Path是一致的,而且应该该IDEA可以直接自动识别出来,然后给该path命名,最后使用该SDK,Apply就可以了。
最后看看能否运行成功,代码如下:
package com.company;public class Main {public static void main(String[] args) {// write your code hereSystem.out.println("Hello World");}
}