When we compile a java program, we get .class files. Can I access these .class files of java core libraries? e.g. can I have access to java.lang.String.class ?
Actually I am doing a research and trying to find branch coverage of some java core libraries. The tool I am using for branch coverage actually instruments the .class files.
Thanks
It's in rt.jar in your JRE directory. You've even got the sources in src.zip. (Or you could just pull the latest version of OpenJDK.)
Also, if you want to instrument the base classes, you'll also need to specify a boot classpath. Look at the -Xbootclasspath option for java: http://java.sun.com/javase/6/docs/technotes/tools/windows/java.html
Sure, it's right there, in jre/lib/rt.jar inside your JAVA_HOME
Yes, the class files are inside the #{JDK INSTALL PATH}/jre/lib/rt.jar archive. Or if you use an IDE like Eclipse you can ctrl+click on any string method, or on "String" in declarations like private String var; to open that class.
Related
I have homework that asks me to use a .class file for my project, but I don't know how to use those files in a .java file. This is the project structure
agentes.class
datos_confidenciales.class
II_Parcial_2007_1.doc
Main.java
numeros.class
I mean, I got to make a class called Main that implements the numeros interface, the numeros interface is in numeros.class file
I've googled but no results.
The .class files simply need to be on the classpath when you compile. How exactly you configure that depends on how you compile (on the commandline or in an IDE; in what IDE).
Here is the documentation for the classpath. Here is a related question for setting the classpath.
The class path is the path that the Java runtime environment searches for classes and other resource files. The class search path (more commonly known by the shorter name, "class path") can be set using either the -classpath option when calling a JDK tool (the preferred method) or by setting the CLASSPATH environment variable. The -classpath option is preferred because you can set it individually for each application without affecting other applications and without other applications modifying its value.
One option is to decompile the class-file, which will get you a java-file of the interface that was compiled.
If you don't want to use a decompiler, write a small application that will reflect the numeros class and prints out all methods and their parameter-types and return-types, so you can implement it.
Of course you could always use an IDE, with the class-file in module-path, your IDE will assist you to implement the interface.
I have modified some core java classes for my own needs eg. using a cache for regexps in Java.lang.String match method.
I want to use these patched classes instead of the default classes in rt.jar that ship with the jre.
I have imported the src.zip sources into eclipse that contains all of the source files of the class files in rt.jar and changed them for my needs. I have then removed rt.jar from the eclipse JRE library config and changed the default VM arguments option for the installed JRE in eclipse to tell the VM to use my own custom core classes>
-Xbootclasspath/p: /media/SSD/workspace/proj/bin/
however when running a simple test program the VM fails to boot with the following>
"Could not find or load main class .media.SSD.workspace.proj.bin."
I also tried using -Xbootclasspath/a: to append instead but it makes no difference.
all the core java classes are in the bin folder with correct directory structure (compiled by eclipse) eg, java/util,java/io etc.
I don't know why it's complaining about a main class as the JRE libraries don't have one and my own main method for the project is in bin/core/boot.class which is detected and set by eclipse in run configurations (and worked fine before).
I was following this guide, which might be a bit out of date and does not use eclipse to patch the core classes>
http://media.techtarget.com/tss/static/articles/content/CovertJava/Sams-CovertJava-15.pdf
could someone point out what i'm doing wrong?
Update/Workaround:
I created a new project with all the core classes then exported it as a new rt.jar file and put it in place of the default one in the jre/lib folder of openjdk and it works. But this means ALL java apps on my machine will use the patched classes rather than just my project which is far from ideal so my question above remains.
There should be no whitespace in your parameter
-Xbootclasspath/p:/media/SSD/workspace/proj/bin/
The parameter to bootclasspath (after :) should be a ; (on Windows) delimited list of folders zips and jars.
Java has interpreted your parameters as empty list of folders to prepend to bootclasspath and a name of class to run, that is why it is complaining that it cannot find class .media.SSD.workspace.proj.bin.
I need to create runnable .jar file programmatically from a string. My decision is to create a .class file from string and add it to the .jar using JarOutputStream.
What API must I use to create the .class file?
Are there any other solutions to create a .jar from the source code?
In order to do that, you can use the Java Compiler API.
There is this excellent tutorial that can walk you through.
To compile code, you need a compiler. You can either use the SunOracle compiler or the Eclipse compiler. Calling the compiler API (both have documented APIs) will produce a .class file in a temporary location. You can then make a jar.
For an example of this sort of thing, start with, for example, the Maven Compiler Plugin, which is a Java module which uses the compiler API. You'll have to find your way into the Plexus compiler module.
I did this before:
CLASSPATH=".:/home/phoenies/jdk1.6.0_17/lib/tools.jar:/home/phoenies/jdk1.6.0_17/lib/dt.jar"
But today an article says I should do this:
CLASSPATH=".:/home/phoenies/jdk1.6.0_17/lib"
If I do so, will it search all the jar files in lib? So it's probably a shorter way?
Since you are using JDK6, you can use classpath wildcards: CLASSPATH=".:/home/phoenies/jdk1.6.0_17/lib/*" will match all JARS inside lib/
Check out http://java.sun.com/javase/6/docs/technotes/tools/windows/classpath.html there's a section called "Understanding class path wildcards"
I think having a CLASSPATH environment variable is wrong for all but the easiest of "Hello, World" tutorials.
The right way is to set the CLASSPATH for every project when you compile and run. Every project is likely to be different, so this makes perfect sense.
IDEs ignore CLASSPATH environment settings; so do all Java EE app servers. It's a relic of Java 1.0. I don't have CLASSPATH set on any machine that I work on.
Learn to script it for the command line. Or use Ant. You'll be glad you did.
Yes, it will search all jar files in lib if you do it the second way. It's pretty odd to see class path being set as specifically as in the first one. I suppose on a server where you wanted to be sure what jars were being loaded, that might be one way to restrict them, but you might run into issues with how long it can be if you had several jars.
Jar files need to be specified by name in the Classpath variable. One thing to note is that the commandline -classpath param is more versatile than the environment variable, as it allows you to set a classpath per application.
In Java 1.6+ you can set the classpath to a directory followed by /* to load all JAR files in that directory. Not just the directory name though - that's for loading class files in that directory and subdirectories.
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