让 Java JDK 在 ubuntu 上编译

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1371453/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 16:17:24  来源:igfitidea点击:

Getting Java JDK to compile on ubuntu

linuxjava

提问by danwoods

I'm trying to start working with Java, but so far haven't been able to get it on my machine properly. I'd really like to be able to compile from command line. After following the instructions herewith no errors I can't compile with javac. Here's what I have so far:

我正在尝试开始使用 Java,但到目前为止还没有能够在我的机器上正确安装它。我真的很想能够从命令行编译。按照此处的说明操作后没有错误,我无法使用javac. 这是我到目前为止所拥有的:

When I enter:

当我输入:

$ java -version

I get:

我得到:

java version "1.6.0_16"  
Java(TM) SE Runtime Environment (build 1.6.0_16-b01)  
Java HotSpot(TM) Server VM (build 14.2-b01, mixed mode)

When I run:

当我运行时:

$ sudo apt-get install sun-java6-jdk

I get:

我得到:

~$ sudo apt-get install sun-java6-jdk  
Reading package lists... Done  
Building dependency tree          
Reading state information... Done  
Suggested packages:  
  sun-java6-demo sun-java6-doc sun-java6-source  
The following NEW packages will be installed:  
  sun-java6-jdk  
0 upgraded, 1 newly installed, 0 to remove and 9 not upgraded.  
Need to get 17.4MB of archives.  
After this operation, 55.7MB of additional disk space will be used.  
WARNING: The following packages cannot be authenticated!  
  sun-java6-jdk  
Install these packages without verification [y/N]? y  
Err http://us.archive.ubuntu.com hardy-updates/multiverse sun-java6-jdk 6-07-3ubuntu2
404 Not Found [IP: 91.189.88.140 80]  
Failed to fetch http://us.archive.ubuntu.com/ubuntu/pool/multiverse/s/sun-java6/sun-java6-jdk_6-07-3ubuntu2_i386.deb  404 Not Found [IP: 91.189.88.140 80]  
E: Unable to fetch some archives, maybe run apt-get update or try with --fix-missing?

When I run:

当我运行时:

$ /media/disk/School/java/hw1$ javac HelloWorldApp,java  </pre>

I get:

我得到:

The program 'javac' can be found in the following packages:  
 * java-gcj-compat-dev  
 * openjdk-6-jdk  
 * gcj-4.2  
 * kaffe  
 * ecj  
 * jikes-sun  
 * jikes-sablevm  
 * j2sdk1.4  
 * jikes-classpath  
 * jikes-gij  
 * gcj-4.1  
 * sun-java5-jdk  
 * jikes-kaffe  
 * sun-java6-jdk  
Try: sudo apt-get install <selected package>  
bash: javac: command not found

When I try to update (using sudo apt-get update) I get:

当我尝试更新(使用sudo apt-get update)时,我得到:

E: The method driver /usr/lib/apt/methods/https could not be found.

Has anyone else encountered this problem? Thanks in advance...

有没有其他人遇到过这个问题?提前致谢...

回答by Matthew Talbert

You can install the JDK on recent versions of Ubuntu by typing this command:

您可以通过键入以下命令在最新版本的 Ubuntu 上安装 JDK:

sudo apt-get install sun-java6-jdk

You might find this easier than attempting to set it up manually.

您可能会发现这比尝试手动设置更容易。

回答by Dave Jarvis

Try this:

试试这个:

  1. Download the Java SDK into $HOME/archives (e.g., $HOME/archives/jdk-6u16-linux-x64.bin).
  2. Extract Java into /opt (or another location if you do not want to use root). For example:
  1. 将 Java SDK 下载到 $HOME/archives(例如,$HOME/archives/jdk-6u16-linux-x64.bin)。
  2. 将 Java 提取到 /opt(或其他位置,如果您不想使用 root)。例如:
cd /opt
chmod 755 $HOME/archives/jdk-6u16-linux-x64.bin
sudo $HOME/archives/jdk-6u16-linux-x64.bin</code>
  1. Create a symbolic link (to ease upgrades):
  1. 创建符号链接(以简化升级):
sudo ln -s jdk1.6.0_16 jdk
  1. Edit $HOME/.bashrc
  2. Append the following lines:
  1. 编辑 $HOME/.bashrc
  2. 附加以下几行:
JAVA_HOME=/opt/jdk
PATH=$PATH:$JAVA_HOME/bin
  1. Reload the environment variables:
  1. 重新加载环境变量:
source $HOME/.bashrc

You should now be able to compile programs.

您现在应该可以编译程序了。

I prefer this method to installing the managed package because uninstalling (or upgrades) never seems to remove all bits of the SDK flawlessly, and it seems to hinder installing multiple versions of the Java Software Development Kit on the same machine at the same time. I have had issues with apt-getand Java in the past. Also, this method allows me to be absolutely certain which version of Java is in use at any time.

与安装受管软件包相比,我更喜欢这种方法,因为卸载(或升级)似乎永远不会完美地删除 SDK 的所有部分,而且它似乎会阻碍在同一台机器上同时安装多个版本的 Java 软件开发工具包。apt-get过去我遇到过Java 和 Java问题。此外,此方法使我可以随时绝对确定正在使用哪个版本的 Java。

If you are not comfortable using rootand /opt, you can use your own account and $HOME/bin/jdkinstead. Change the .bashrcfile accordingly.

如果您不习惯使用rootand /opt,您可以使用自己的帐户 and$HOME/bin/jdk代替。相应地更改.bashrc文件。

Remove any version of Java you previously had installed. You might need to restart your terminal session.

删除您之前安装的任何 Java 版本。您可能需要重新启动终端会话。

This works for all versions of Java since at least Java 1.2.

这适用于至少从 Java 1.2 开始的所有 Java 版本。

回答by Vineet Reynolds

Usually you will find java in the PATH and not javac in a standard Ubuntu installation. This is primarily because of the gcj package that gets installed. Symlinks are also created that can be updated using the update-alternativesscript.

通常你会在 PATH 中找到 java 而不是在标准的 Ubuntu 安装中找到 javac。这主要是因为安装了 gcj 包。还创建了可以使用update-alternatives脚本更新的符号链接。

After an installation of Sun JDK, you are required to update the symlink to java, and this is usually done via a command similar to the one below

安装 Sun JDK 后,您需要将符号链接更新为 java,这通常通过类似于下面的命令来完成

sudo update-alternatives --config java
须藤更新替代品--config java

If hardlinks to the location of (Sun) java is not present, you can create it using a command similar to

如果 (Sun) java 位置的硬链接不存在,您可以使用类似于

sudo update-alternatives --install /usr/bin/java java /usr/lib/jvm/jdk1.6.0_07/jre/bin/java 300
须藤更新替代品 --install /usr/bin/java java /usr/lib/jvm/jdk1.6.0_07/jre/bin/java 300

In the case of javac, you can create a symlink, again using update-alternatives using:

在 javac 的情况下,您可以创建一个符号链接,再次使用 update-alternatives 使用:

sudo update-alternatives --install /usr/bin/javac javac /usr/lib/jvm/jdk1.6.0_07/bin/javac 300
须藤更新替代方案 --install /usr/bin/javac javac /usr/lib/jvm/jdk1.6.0_07/bin/javac 300

This will make javac available on PATH, just like java.

这将使 javac 在 PATH 上可用,就像 java。

Of course, there is option of updating the PATH variable via a simple export or via changes to the shell configuration file.

当然,可以选择通过简单的导出或通过更改 shell 配置文件来更新 PATH 变量。

回答by djeikyb

I just ran into this problem while installing the html validator from http://validator.nu. To install sun-java6-jdkI had to add this repository: deb http://archive.canonical.com/lucid partner

我在从http://validator.nu安装 html 验证器时遇到了这个问题。要安装sun-java6-jdk我必须添加这个存储库:deb http://archive.canonical.com/lucid partner

To complete the validator install, I had to set (export) my $JAVA_HOMEto /usr/lib/jvm/java6-sun.

要完成验证器安装,我必须将我的设置(导出)$JAVA_HOME/usr/lib/jvm/java6-sun.

Do change "lucid" to whatever is appropriate for your Ubuntu edition.

将“lucid”更改为适合您的 Ubuntu 版本的任何内容。