view java source code - java

Is any way to view the actual code behind the default classes in Java (java.awt.*;, javax.swing.*;, etc) to see exactly what it is that is happening?
I don't mean just the documentation, or a list of methods, etc, but the source code itself in full detail (in other words, what could be used to create an exact copy of an entire method/class if copied and pasted into the code for a program in Java).

The Java source code for all the API classes is shipped in the JDK installer in a file named src.zip. It's often just sitting in your install directory. Unzip it, and have a look.
If it's not there, you may have chosen not to install it; reinstall the JDK and watch for the "source code" option, making sure to include it.

I frequently use http://docjar.com for this purpose.
Example: I want to see the source code for String. Search for the FQCN, java.lang.string (using the "Package/class Name" option). Click the result you want, then click the source link at the top of the page. Voila: http://www.docjar.com/html/api/java/lang/String.java.html

src.zip usually comes with JDK.

I also find http://www.codeatlas.com to be a great place to look for JVM based source code in general. For example, if you are ever looking for how java.awt.ActionEvent is implemented you can find it here:
http://www.codatlas.com/project/L_fXVCOhW4_lzXEd3R5DNQ__/master/src/share/classes/java/awt/event/ActionEvent.java?keyword=ActionEvent&line=59
What's nice about it is that it gives an IDE like browsing experience by adding cross-reference and syntax highlighting.

For those who use linux distributions, like ubuntu, archlinux, etc. You can always get openjdk source code via corresponding package management system. This means you can choose a faster mirror to get the source code.
For ubuntu users:
$ sudo apt-get install openjdk-7-source
For Archlinux users:
$ sudo pacman -S openjdk7-src
The source will be located in the $JAVA_HOME, somewhere like /usr/lib/jvm/java-7-openjdk/src.zip.
Other distributions will have likewise package names, so not listed here.

1) Use Eclipse to attach the source code in C:\users\program files\jdkx.x
2) Use GrepCode if you are just browsing the code and to save memory used by heavy weight eclipse on your machine

Related

Setting up Cloud9 IDE to compile and run Java?

I have tried posting this request in Cloud9's forums, but since it never posts, I am hoping someone here could give me a hand. (The forum keeps saying my question is being submitted for review, but then never shows up).
I can't get Java code to compile and run. I've Googled the issue and read other posts in their limited forums, but I can't get it to compile and run a simple Hello World! program. My .java filename matches the class and the code is fine.
I have tried putting my .java files in the following directories:
test/java/
test/src/
I just get this:
bash: src/HelloWorld.java: No such file or directory
I tried this: http://www.programmingforliving.com/2014/09/java-coding-with-cloud9-ide.html
But I just get:
Running HelloWorld.java
Error: Could not find or load main class HelloWorld
Any suggestions? Given I'm essentially a Java noob I am hoping the solution is fairly simple.
Without you pasting in your code it's tough to tell but make sure your class and filename are both titled HelloWorld. Make sure your src folder is in your main workspace folder so your file should be at /home/ubuntu/workspace/src/HelloWorld.java.
Also, this documentation looks pretty similar to the blog you referenced but still worth a look https://docs.c9.io/docs/custom-runners.
I think that Codenvy is much better in java than Cloud9. It provides easy build & run configuration, code completion, project sharing with permissions or factory (creates clone of your project with one click in minutes).
Take a look at official java spring factory. This link opens temporary(personal) java project ready to code, build & run.
Also Codenvy has project wizard that has a lot of project types including java console app (uses maven).
Ok, decided to screw around with it and figured it out... (then saw that Brady Dowling had posted a link that explained in better detail what needed to be changed).
At any rate, I noticed a few things in Cloud9.
Even after making the changes and saving, it would still not compile correctly. I had to close my session of C9 and go back in. Then it would work.
After compiling, and trying to run the program, it wasn't automatically picking the Java runner I made. So if you are running into issues, make sure the Java (or whatever you decided to name it) runner is selected in the console.
You have to compile and run separately. Kind of annoying, but at least it works. Unlike C/C++ in C9, it won't compile and run by simply hitting 'Run'. If you do, it creates an error saying the .class doesn't exist. Simple as pressing F7 to build, then F5 to run.
Anyway, I took screenshots of what I had to change to get it working. Essentially, just the directories it looks for the .java file in (the right arrows) and the directory it looks for/compiles the .class file to (the left arrows). Since I was putting the .java and .class in the same directory (java), I just changed both to 'java'. In the links Brady and myself posted, they used .bin (for .class) and src for (.java).
1st picture is the Builder file (I called it javac) and the 2nd picture is the Runner file (I called it Java).
Anyway, I hope this helps someone out. I am really liking C9 and it perfectly suits my novice needs at the moment. I like the ability to dabble in multiple languages to see what I like without having to switch applications. The real-time live preview HTML/CSS like Brackets is another huge plus for me.
Regards
For what I understand, C9 workspaces don't come with jdk installed. But I always use these commands:
sudo apt-get update
(wait a while for it to finish processing. Then)
sudo apt-get install default-jdk
(Then It will ask you for some permission. Type "Y")
And then it compiles and runs normally using the terminal.
Sometimes you will also need to install a package to get javac.
Example:
sudo apt-get install openjdk-7-jdk
The java command works right of the box. You may java -version to see that it is Ubuntu running. You can also locate the java binary and see that it is amd64. That is all you need to know to download jdk manually from Oracle.com, like
wget --no-check-certificate --no-cookies --header "Cookie: oraclelicense=accept-securebackup-cookie" http://download.oracle.com/otn-pub/java/jdk/8u92-b14/jdk-8u92-linux-i586.tar.gz
tar xf jdk-8u92-linux-i586.tar.gz
and voila, jdk-1.8.0_92/bin/java(c) can be executed! It allows you to compile java files.

GWT project and Eclipse linked resources

I have to use a Javascript file in my GWT Project. This Javascript is in a common library project and I deploy it together with my GWT Project using ANT.
So, I have no problem in production environment: but I cannot test it in development phase.
I tried to create an Eclipse link to Javascript resource but seem that GWT "can't see it".
Some behavior with other kind of resources (images, css etc.).
Is it a bug or is there another way to do?
I'm using Eclipse Juno, GWT 2.5.0 and Debian 7.0.
Thank you.
Can you just use script tag in your project's .html file?
If you're using some kind of source control, it usually has a way to make a link to a dependent project or file. Simply include that link in your GWT Project, and reference the file through there.
If you can't or don't want to do it through your source control, do it through your OS. Since you're using Linux, simply make a symbolic link to the common file/folder using ln -s (if you were using Windows, you'd need to run mklink from the command line), and reference the file that way.
In either case - source control or OS - you'll be able to see the file(s) when you refresh your project in Eclipse, and modifying one will modify the other in its own directory.
Edit - information on symbolic links in CVS
I haven't played with CVS in quite some time, so can't speak much about its capabilities for symbolic links. A bit of googling said it's not supported, though there are workarounds. One workaround is to add script files that run during checkout. That sounds like it may still be tough to make OS-agnostic. I did find one site that mentioned using module aliases to get the same result. Maybe that will give what you need. An excerpt from the site follows:
One common way to handle situations like this in CVS is to alias the
collection in a modules file rule. -Checkout the "CVSROOT" module and
you'll find the "modules" file; simply change it and check it in like
anything else, with the exception that when you check in CVSROOT files
they "activate" at the same time. The example below may look a little
kludgy, and it is because AFAIK you can't redefine a directory and alias
it at the same time, sadly. I'll use a typical Java situation as its
package system lends itself well to this kind of thing:
Real module directories are "a", "b", and "common"
Directory alias for all common srouce
_common_src_all -d src/com/mycompany/common common/src/com/mycompany/common
Full "A" project including common
a_all &a &_common_src_all
Full "B" project including common
b_all &b &_common_src_all

How to update rt.jar file?

I want to add some class files to rt.jar. How am I able to do that?
Your question indicates you have some misunderstanding of the java platform.
First of all you need to know what the rt.jar is and what it does:
rt.jar is the jar that contains all the classes necessary for the java runtime. Hence it's name rt.jar
Now that you know that, you need to know how your java program runs:
Your java program, all your jars and classes are executed by the java virtual machine.
So as you can see the code you write & the rt.jar which is used by the java run time are completely separate and should remain so.
If you need some functionality you should add it to your jar.
Do not update it. Why do you want to update it?
Well anyhow if you want to update it, I know one way, You can open jar file in winrar and paste updated .class files in archive. But your jar may get in inconsistent state. Do it at your own risk.
You can use this command:
jar uf jar-file input-file(s)
Refer the link for details:
java.sun.com/docs/books/tutorial/deployment/jar/update.html
The best way to update the rt.jar is to install a newer version for Java. ;)
If you want to add your own classes in new packages, you can add these to a jar which is in your class path. This is preferable to changing existing classes.
If the only option is to change existing classes, you can create a "patch" in a jar which you prepend to your boot class path, or you can add the jar to an lib/endorsed directory. I wouldn't do this for production code, only for your own testing.
I've tried jar.exe with the u and 0 (i.e. zero) options and that gets the closest to looking like the original rt.jar file but if I've updated the JDK's JRE's rt.jar I have problems with compiling and jarring after the update. No idea why! Simply running a program with the JRE seems to work.
I also tried -Xbootclasspath/p but couldn't get it to work.
Looking at Replace a class within the Java class library with a custom version I see that there are legal problems with distributing an altered rt.jar to your customers, even if you could figure out how to do it correctly. So I plan to take the advice in that page and create a java agent. That's apparently legal and works.
One reason a person might want to modify rt.jar is to add debugging information after compiling the source that comes with the JDK with the -g option. One may also want to patch something. These would be for one's own use, of course.

Java application does not work properly outside IDE's directory

When I run it with NetBeans it's all OK. When I copy dist directory contents and run .jar, some stuff gets buggy. Most important, JTable editing gets messy, some fields lose focus only when you hit ESC (if you did hit ENTER before, changes are accepted, otherwise they are not, but you need ESC in both cases) and similar weird stuff. I got a feeling that I'm missing something obvious...
P.S. files are compiled automatically on save (NetBeans feature) if that matters.
Edit: when I just go to dist dir and run .jar, it works too. Problems begin when I copy dist dir out of NetBeans projects dir... can it be that some dependencies get broken or something?
Edit 2 (reply):
This problem was happening in my computer (Ubuntu 9.04), in my Windows XP inside Virtual Box and in another (real) computer with Windows XP. When I run it from console with java /path/to/main.jar it throws mainClassNot found exception and does not launch at all. When I run it with java -jar /path/to/main.jar, it works of sorts, but when it comes to said situations, it throws java.lang.NumberFormatException: null.
The only place I use NumberFormat (on table update) is:
DecimalFormat parser = new DecimalFormat("0.00");
And, possibly, this:
currencyFormatter = NumberFormat.getCurrencyInstance( Locale.getDefault() );
Where default locale is set to
Locale.setDefault(new Locale("lt", "LT"));
Java version is 1.6.0_18, both JDK used by NetBeans and JVM in said machines.
In NetBeans go to your project's properties (File > Project Properties). Go to the Libraries tab. Click Manage Platforms and see the value for the Platform Folder.
From a console, run <platform folder>\java -version.
Now try it again without the full path; just java -version.
I would expect these are returning different values.
The path used by the IDE comes from the platform definition which, by default, is created when NB is installed and never updated. The path used in the console is from the windows PATH environment variable. This is updated whenever Java is installed and will, over time, diverge from the path used by the IDE.
A good rule of thumb is when Java prompts that an update is available it's time to add a new Java Platform in NetBeans.
I usually keep several platforms around. At a minimum:
latest versions of 1.4.2, 1.5.0, 1.6.0, and an old version of 1.6.0 (currently u4, the version we recommended in our first product release).
Have you tried something as mundane as making a clean build to make sure all new changes and resources are copied to the dist library?
Interesting... JAR's are stored in ZIP format, so you could try comparing the JAR that you've compiled with Netbean's JAR (if you can find it) to see exactly what is different.
Different JRE versions? That would be my guess, looking at your symptoms.
There are different ways to do this, but you could get the complete details of both the processes (one launched by NetBeans and one without) using jconsole (jdk_dir/bin/jconsole.exe). This would give you the JRE, loaded jars, etc that you could then compare...
HTH...

Can I add classes to sun's rt.jar file?

I downloaded the Javax.mail package. I have jdk1.6.0_11.
Problem is...I cannot get javac or java to find those classes!
I can get apps to compile using JCreator LE ( by adding the mail jar
to its search list ) but, when I try to run the app in a command window,
it fails.
Can I add these new classes to the rt.jar without
hurting my jdk installation?
I know java has it wired up to look there for classes.
(And, the mail classes are inside a javax package - seems like
they could reasonably be added to the javax folder in rt.jar..
Thanks!
Phil D'
No you can't, nor should you.
Instead, figure out the problem with your classloader (probably paths?). You'll need that for the next library you need to access.
Messing with rt.jar means you can't run on any other JVM.
You should either specify the jar file in your classpath: preferably on the command line with the -cp option, but possibly with the CLASSPATH environment variable.
Alternatively, you can specify its directory in the java.ext.dirs system property. For more details, see the documentation for the extensions mechanism.
You shouldn't be messing around with rt.jar. That's very definitely not the way to make extra jar files available - it's akin to trying to add Microsoft Word to the Windows kernel ;)
Adding things to rt.jar seems like a bad idea, even though its possible and easy to accomplish.
Try compile your application from the command line like this:
javac -cp <path_to_3rd_libs>/jarfile.jar . MainClass.java
If the compiler still complains about the javax.mail package try to unpack/examine the jar file to see that javax.mail package (and its expected content) is there.
(On windows its easy to examine a jar file using 7zip.)
Most definitely no.
If you post the command you are running from the command line we will be able to point you on the right direction, but most likely you are just missing a classpath parameter.
java -classpath /path/to/mail.jar MyClass
You need to understand the CLASSPATH concept which allows you to add individual classes and jar files containing classes to the "universe" of defined classes available for the code you want to compile and/or run. It is similar in idea to the PATH variable in the Windows world.
For the Windows command line this is the documentation:
http://java.sun.com/javase/6/docs/technotes/tools/windows/classpath.html
The Java Tutorial surprised me by not being well-written for this particular concept:
http://java.sun.com/docs/books/tutorial/essential/environment/paths.html
You most likely need something along the lines:
C:> set CLASSPATH=c:\javamail\first.jar;c:\javamail\second.jar
after which both java and javac should know about these classes

Categories