The first Java program under MAC

Reprinted from https://blog.csdn.net/qq_30745645/article/details/60756265, thanks to the blogger

1. JDK installation

搜索Java download,第一个就是JDK的[官方下载地址](http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html). 我这里用的是MAC版的, 下载的是dmg文件, 直接傻瓜式安装即可, 类似的教程很多, 就不赘述了.
  • 1
  • 2

2. Environment variable configuration

傻瓜式安装后, JAVA_HOME路径应该已经在可以通过命令行查看
  • 1

 cd /Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/

After confirming the path, first create a .bash_profile, which requires permissions, I use the following method

touch .bash_profile

Edit .bash_profile

you .bash_profile

Edit with i or a, paste the following code

JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.9.0_161.jdk/Contents/Home

CLASSPAHT=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar

PATH=$JAVA_HOME/bin:$PATH:

export JAVA_HOME

export CLASSPATH

export PATH
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

esc -> :wq -> enter to finish editing and saving

Read and execute .bash_profile

source .bash_source

Of course, this is an operation that is valid for the current user. After switching users, this configuration is invalid. There is another configuration method below that can solve this problem.

cd /etc 
sudo vi profile

Enter the administrator password here. After execution, you will see that the content of the file is

# System-wide .profile for sh(1)

if [ -x /usr/libexec/path_helper ]; then
        eval `/usr/libexec/path_helper -s`
fi

if [ "${BASH-no}" != "no" ]; then
        [ -r /etc/bashrc ] && . /etc/bashrc
fi
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

Add three lines of code below

JAVA_HOME="/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home"

export CLASSPATH=.:$JAVA_HOME/lib:$JRE_HOME/lib:$CLASSPATH

export PATH=$JAVA_HOME/bin:$JRE_HOME/bin:$PATH
  • 1
  • 2
  • 3
  • 4
  • 5

Since this system file is readonly, use esc -> wq! to force save and exit, and then execute the profile file.

source profile

Almost here, the configuration is completed, and you can use it happily. In fact, for novices, knowing how to use it is the most important thing, especially for the configuration things, you can use it. It doesn’t make much sense if you don’t understand it. feel)

3. Run the first Helloworld program

我选择的编译工具是Sublime text 3, 仅仅是因为这个能编译很多种语言, 当然这个工具需要配置的有点多, 也有很多教程, 就不多说了. 初学者可以参考[Sublime text初次安装及配置](http://blog.csdn.net/u011272513/article/details/52088800)


假装已经配置好了这些东西, 打开Myeclipse,贴入第一个Java程序代码, 右下角选择java.
  • 1
  • 2
  • 3
  • 4
  • 5
public class HelloWorld{
    public static void main(String[]args){
        System.out.println("Hello HelloWorld");
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5

Well, Command+B selects javaC to compile, if no error is reported, it is impossible.

There are a few simple places to pay attention to. In short, the saved file name should be consistent with the class name. 
cmd+S is saved as a HelloWorld.java file, that is, xxx.java, and then public class xxx , the name of this place Be consistent.

如果自己手打这段代码出现错误, 建议检查一下大小写, 笔者开始就因为String[] 写成string[]报了个错.

好了, 假装这些操作都没问题, cmd+B, 控制台会打印

[Finished in 0.6s]

这时候去你存放HelloWorld.java的路径下, 会发现生成了一个HelloWorld.class的文件, 进入这个路径下, 执行

cd 存放路径 
java HelloWorld

好了, 是不是已经成功输出了第一个HelloWorld程序, ^_^

4.小结

可能和看到这个文章的大家一样, 笔者也是个萌新, 仅可能比大家多一点编程经验, 有些别的语言的编程经验, 在这里记个笔记, 顺便共享一下自己踩过的坑, 给大家提供些方便. 虽然我觉得也没几个人闲着无聊用MAC来学习Java, 能帮助到你, 并且开心就好了, 以后有时间会继续更新下去, 就当是自己的笔记了.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325375109&siteId=291194637