javac 在 ubuntu 终端中不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16267894/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
javac does not work in ubuntu terminal
提问by Prasanna
When I try to compile a java program with javac
, I get an error:
当我尝试使用 编译 java 程序时javac
,出现错误:
The program 'javac' can be found in the following packages:
* default-jdk
* ecj
* gcj-4.6-jdk
* gcj-4.7-jdk
* openjdk-7-jdk
* openjdk-6-jdk
Try: sudo apt-get install <selected package
When I try java -version, it says,
当我尝试 java -version 时,它说,
java version "1.7.0_21"
OpenJDK Runtime Environment (IcedTea 2.3.9) (7u21-2.3.9-1ubuntu1)
OpenJDK 64-Bit Server VM (build 23.7-b01, mixed mode)
When I try which java I get /usr/bin/java
当我尝试使用哪个 Java 时 /usr/bin/java
How do I compile my java program from commandline?
如何从命令行编译我的 java 程序?
采纳答案by HeatfanJohn
As mentioned by @Blorgbeard you most likely do not have a Java JDK (Java Development Kit) installed. See this web pagefor more information about the various versions of Java available for Ubuntu.
正如@Blorgbeard 所提到的,您很可能没有安装 Java JDK(Java 开发工具包)。有关可用于 Ubuntu 的各种 Java 版本的更多信息,请参阅此网页。
The following command will install the OpenJDK version of the Java JDK:
以下命令将安装 Java JDK 的 OpenJDK 版本:
apt-get install default-jdk
回答by user2862244
I have Ubuntu 12.10, with java "1.6.0_27", this command makes javac available:
我有 Ubuntu 12.10,使用 java“1.6.0_27”,此命令使 javac 可用:
sudo apt-get install default-jdk
Then compile it like this:
然后像这样编译它:
el@apollo:~/retreat3$ javac HelloWorld.java
el@apollo:~/retreat3$ java HelloWorld
Hello, World!
回答by GlenM
I was having the same problem. I used the following command
我遇到了同样的问题。我使用了以下命令
sudo apt-get install openjdk-7-jdk
sudo apt-get install openjdk-7-jdk
The app compiled after that. Not sure what the difference is to the above solutions. Maybe I wouldn't of had to specify a version.
之后编译的应用程序。不确定与上述解决方案有何不同。也许我不必指定一个版本。
回答by Sachin from Pune
just copy and paste on your Terminal
只需复制并粘贴到您的终端上
sudo apt install openjdk-8-jdk-headless
sudo apt 安装 openjdk-8-jdk-headless
回答by rotarydial
In my case, I had the JDK installed, but I'd installed it manually so it wasn't added to the path. Once I added these lines to my .zshrc file (I'm using zsh; if you're using bash, add them to .bash_profile) and relaunched the terminal, it worked:
就我而言,我安装了 JDK,但我手动安装了它,所以它没有添加到路径中。一旦我将这些行添加到我的 .zshrc 文件中(我使用的是 zsh;如果您使用的是 bash,请将它们添加到 .bash_profile)并重新启动终端,它就起作用了:
# Java environment variables
export JAVA_HOME='/opt/jdk/jdk1.8.0_241' #change accordingly; this needs to match your JDK's location!
export PATH=$PATH:$JAVA_HOME/bin
回答by arnav
Actually, java command was working on my ubuntu 16.04 but not javac. At first, I fiddled around with the environment paths and all as i thought i had all java packages installed and the issue was with setting the environment path variable.
实际上,java 命令在我的 ubuntu 16.04 上工作,但不是 javac。起初,我摆弄环境路径,因为我认为我已经安装了所有 java 包,问题在于设置环境路径变量。
Then i tried the command update-alternatives --list java
which returned a path but update-alternatives --list javac
did not.
This explained to me that javac needed to be installed so I ran sudo apt-get install default-jdk
然后我尝试了update-alternatives --list java
返回路径但 update-alternatives --list javac
没有的命令。这向我解释了需要安装 javac 所以我跑了sudo apt-get install default-jdk
But incase you get a path in running the update-alternatives --list javac
then i suggest you should try setting up your $JAVA_PATH or creating symbolic links. Help to that can be found on the page unable to run javac on Ubuntu.
但是,如果您在运行时获得了路径,update-alternatives --list javac
那么我建议您应该尝试设置您的 $JAVA_PATH 或创建符号链接。可以在无法在 Ubuntu 上运行 javac的页面上找到帮助。