附上 Java 源代码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1157597/
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
Attach the Java Source Code
提问by hello_world_infinity
I cannot for the life of me attach the java source code to eclipse so I can see the inner workings of the language. Not even something as simple as the String Class.
我一生都无法将 java 源代码附加到 eclipse 中,这样我就可以看到该语言的内部工作原理。甚至不是像 String 类那样简单的东西。
when I run java -version this is what I have:
当我运行 java -version 这就是我所拥有的:
java version "1.6.0_14"
Java(TM) SE Runtime Environment (build 1.6.0_14-b08)
Java HotSpot(TM) Client VM (build 14.0-b16, mixed mode, sharing)
I am downloading the java souce from: http://download.java.net/jdk6/source/
我正在从以下位置下载 java 源:http: //download.java.net/jdk6/source/
And in eclipse when I attach it It says:
在 eclipse 中,当我附加它时它说:
The JAR file "C:\Program Files\Java\jre6\jdk-6u14-fcs-src-b08-jrl-21_may_2009.jar"
has no source attachment.
What am I doing wrong?
我究竟做错了什么?
采纳答案by VonC
Normally, if you have installed the JDK6u14, eclipse should detect it and declare it automatically in its "installed JRE" list.
通常,如果您已经安装了 JDK6u14,eclipse 应该会检测到它并自动在其“已安装的 JRE”列表中声明它。
If not, you can add that JDK through "Windows/Preferences": Java > Installed JREs
:
如果没有,您可以通过“Windows/Preferences”添加该 JDK Java > Installed JREs
::
Just point to the root directory of your JDK installation: it should include the sources of the JDK (src.zip
), automatically detected and attached to rt.jar
by eclipse.
只需指向 JDK 安装的根目录:它应该包含 JDK ( src.zip
)的源代码,rt.jar
由 Eclipse自动检测并附加到。
回答by Thorbj?rn Ravn Andersen
The easiest way to do this, is to install a JDK and tell Eclipse to use it as the default JRE. Use the default install.
执行此操作的最简单方法是安装 JDK 并告诉 Eclipse 将其用作默认 JRE。使用默认安装。
(from memory)
(从记忆里)
Open Window -> Prefences. Select Installed Java runtimes, and choose Add. Navigate to root of your JDK (\Programs...\Java) and click Ok. Then select it to be the default JRE (checkmark).
打开窗口 -> 首选项。选择已安装的 Java 运行时,然后选择添加。导航到 JDK 的根目录 (\Programs...\Java),然后单击 Ok。然后选择它作为默认 JRE(选中标记)。
After a workspace rebuild, you should have source attached to all JRE classes.
工作区重建后,您应该将源附加到所有 JRE 类。
回答by JAB
Hold ctrl key and then click on class of which you want to see the inner working (for ex: String
) then you will find there button "Attach Source". Click on it. Then click on External Folder. Then browse to your jdk location, per instance C:\Program Files\Java\jdk1.6.0
. That's it.
按住 ctrl 键,然后单击要查看内部工作的类(例如:),String
然后您会在那里找到“附加源”按钮。点击它。然后单击外部文件夹。然后浏览到您的 jdk 位置,每个实例C:\Program Files\Java\jdk1.6.0
。就是这样。
回答by Joseph Lust
You don't necessarily need to addthe source, but you rather may need to removea JRE that does not have the source attached.
您不一定需要添加源,但您可能需要删除没有附加源的JRE。
On looking at the "installed JRE's" I saw that my JDK was setup properly with source, but the default JRE on the machine had no sources. Eclipse was defaulting to that when looking for source.
在查看“已安装的 JRE”时,我看到我的 JDK 已使用源正确设置,但机器上的默认 JRE 没有源。Eclipse 在寻找源代码时默认使用它。
I just used the removebutton to expel the JRE, leaving my JDK. I then hit F3 and the source was there. Yeah!
我只是使用删除按钮来驱逐 JRE,留下我的 JDK。然后我按 F3,源就在那里。是的!
回答by Thiago
Just remove the JRE in Preferences>Java>Installed JRE and add the folder of your JDK. If you just add JDK but still leave JRE it won't work
只需在 Preferences>Java>Installed JRE 中删除 JRE 并添加您的 JDK 文件夹。如果你只是添加 JDK 但仍然离开 JRE 它将不起作用
回答by schnatterer
What worked for me (with JDK7) was the following:
对我有用的(使用 JDK7)如下:
- Download the openjdk-xy-sources.jarfrom GrepCode,
- rename the file to src.zip,
- copy src.zipto the root folder of the JRE/JDK added to eclipse (the one with binand libfolders in it)
- restart eclipse
- 从GrepCode下载openjdk-xy-sources.jar,
- 将文件重命名为src.zip,
- 将src.zip复制到 eclipse 中添加的 JRE/JDK 的根文件夹(里面有bin和lib文件夹)
- 重启日食
Alternatively, if you don't want to write to your JDK folder, you could also attach src.zipto (at least) rt.jarin eclipse in the Window| Preferencesmenu in Java| Installed JREs.
或者,如果您不想写入 JDK 文件夹,您也可以将src.zip附加到(至少)Eclipse 中Window| 的rt.jar。Java 中的首选项菜单| 已安装 JRE。
If you're not comfortable with downloading the sources from GrepCode, you could also get them from openJDK directly. This requires requires a bit more effort, though. Replace step one above by the following steps:
如果您不习惯从 GrepCode 下载源代码,也可以直接从 openJDK 获取它们。不过,这需要更多的努力。用以下步骤替换上面的步骤一:
- Download the JDK sources from here,
- extract all folders from the zip file's *openjdk\jdk\src\share\classes* folder,
- create a file src.zipand add these folders to it,
- copy src.zipto the root folder of the JRE/JDK added to eclipse (the one with binand libfolders in it)
- restart eclipse
- 从这里下载 JDK 源代码,
- 从 zip 文件的 *openjdk\jdk\src\share\classes* 文件夹中提取所有文件夹,
- 创建一个文件src.zip并将这些文件夹添加到其中,
- 将src.zip复制到 eclipse 中添加的 JRE/JDK 的根文件夹(里面有bin和lib文件夹)
- 重启日食
A third alternative to acquire src.zipis to download the unofficial OpenJDK builds from here. The src.zipis contained within the downloaded zip.
获取src.zip 的第三种方法是从这里下载非官方的 OpenJDK 版本。该src.zip包含下载的zip内。
回答by praveenak
To attach JDK source so that you refer to Java Source Code for code look-up which helps in learning the library implementation and sometimes in debugging, all you have to do is:
要附加 JDK 源代码以便您参考 Java 源代码进行代码查找,这有助于学习库实现,有时还有助于调试,您所要做的就是:
In your Eclipse Java Project > JRE Reference Librarylocate rt.jar. Right click and go to Properties:
在您的Eclipse Java 项目 > JRE 参考库中,找到rt.jar。右键单击并转到属性:
Select "Java Source Attachment"on the right and to the left select "External Location"and click on "External File"Button and locate "src.zip"file in your $JAVA_HOMEpath in my case for my windows machine src.ziplocation is: C:/Program Files/Java/jdk1.7.0_45/src.zip.
选择右侧的“Java Source Attachment”,左侧选择“External Location”,然后单击“External File”按钮并在$JAVA_HOME路径中找到“src.zip”文件,对于我的 Windows 机器src.zip位置是:C:/Program Files/Java/jdk1.7.0_45/src.zip。
You are done now! Just Ctrl + clickon any Java library Class in your project code to look-up the source code for the java class.
你现在完成了!只需按 Ctrl + 单击项目代码中的任何 Java 库类即可查找 Java 类的源代码。
回答by andro83
Old question, be here's what I ended up using Java Source Attatcher plugin: http://marketplace.eclipse.org/content/java-source-attacher#.U85j4rF-N7s
老问题,这是我最终使用 Java Source Attacher 插件的结果:http://marketplace.eclipse.org/content/java-source-attacher#.U85j4rF-N7s
It can provide source for different opensource projects. One weird thing thou, if you have libraries under libs folder, you cannot attach from there, but you need to attach source clicking on jar displayed under "Libraries from external".
它可以为不同的开源项目提供源代码。一件奇怪的事情,如果你在 libs 文件夹下有库,你不能从那里附加,但你需要附加源点击“来自外部的库”下显示的 jar。
回答by tigger2011
The option provided by"praveenak" can also be applied to any other jar files with source file provided. For example, for JavaFx, we right click jfxrt.jar, select "Properties" and enter jdk1.8.0_05/javafx-src.zip for "Path" under External location.
"praveenak" 提供的选项也可以应用于任何其他提供源文件的 jar 文件。例如,对于JavaFx,我们右键单击jfxrt.jar,选择“属性”,在外部位置下的“路径”输入jdk1.8.0_05/javafx-src.zip。
回答by Weslor
Easy way that just worked for me:
对我有用的简单方法:
- Check if you have a jdk installed (it is usually in "Program Files\Java", if you don't have one, install it.
- Link it to eclipse. In Preferences --> Java --> Installed JREsremove the JRE installed and add the JDK.
- 检查您是否安装了 jdk(通常在“Program Files\Java”中,如果没有,请安装它。
- 将其链接到日食。在Preferences --> Java --> Installed JREs 中删除已安装的 JRE 并添加 JDK。
Then the project will detect it and add the new libraries from the JDK. At the end of this process it looks like the screenshot I attach.
然后项目将检测它并从 JDK 添加新库。在此过程结束时,它看起来像我附上的屏幕截图。