• Post Reply Bookmark Topic Watch Topic
  • New Topic

How can i prepare my own rt.jar by changing jdk`s *.java source file. Example given.  RSS feed

 
Mohan Gaddam
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My self is Mohan Kumar Gaddam, from India-- Bangalore, working as java developer since 4 years.

just for curiosity I wanted to change existing String.java and let it work in the way i wanted. (Not just String.java, but any other collecton f/w classes for)

Scenario i performed:
1) I have taken the String.java from src\java\lang folder of JDK,
2) changed the existing toUpperCase() method as below:
            public String toUpperCase() {
                return toLowerCase(Locale.getDefault());   //here existing code is  return toUpperCase(Locale.getDefault());
            }

3) And i compiled it, prepared new rt.jar with every other class, and also prepared alt-string.jar
4) Replaced the newly prepared jars in <JAVA_HOME>\jre\lib
5) And written a sample Test class in which created a String object and called the above method and, tried to compile the code.
 bang...!!!, it kicked me as below:
       java.lang.InternalError
                at sun.misc.Launcher.getFileURL(Launcher.java:479)
                ........
                at java.security.AccessController.doPrivileged(Native Method)
                ........
                at java.lang.ClassLoader.getSystemClassLoader(ClassLoader.java:1319)

(please note, the method i changed is meaning less-- but i am not checking for meaning, im trying to understand the things)
Please note im not changing any method signature, I just want to change the method behavior for altering performance.

Here are the questions:
1) why i can not create a customised rt.jar?
2) How sun(oracle) prepares rt.jar?
3) is there a way that i can do the same task to give access to the current String class?

Please dont instruct me to extend or implement or deligate pattern etc,(String can not be extended but other classes can be)

It would be great if somebody could solve my problem.
eagerly waiting for your reply..


Regards
Mohan
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16057
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

Why are you trying to change the behaviour of a method in the String class? you should not be doing that. It will make your special, modified version of Java incompatible with standard Java. I don't know the details of Oracle's license for Java, changing the standard runtime classes might not even be allowed by the license. As you've noticed, there are special security checks which prevent you from easily changing the standard runtime classes.

Are you trying this just out of curiosity, or do you have some actual problem which you thought you could solve by modifying the standard String class? If it's the latter, then please explain what your actual problem is, and we can see what the best way is to solve it.
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The only way you can do that without violating the user agreement is to write your own Java installation. Shouldn't take more than two years. You will need to make it conform to the entire Java Language specification.

Please don't use coloured text which is difficult to read; I shall change it to black.
And welcome to the Ranch
 
Mohan Gaddam
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote: And welcome to the Ranch

Thanks for the welcome Ritchie.

so how oracle or sun prepared rt.jar, and do they sign it, I did not see any such info in manifest or otherfiles.
At the end of the day oracle has to prepare rt.jar through jar -cvf etc etc for every release, then where they will be maintaining the security info of jar,
is it in javac or java executables?

Thanks
mohan
 
Mohan Gaddam
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:Welcome to the Ranch.


Thanks for the welcome Jesper
Im doing it out of curiosity for now.. as i specified The modification is meaning ful, but doing just to check the case.
In case if i have to chaneg the collection classes without extending for performance, this might be the better way.

Thanks
Mohan
 
Anayonkar Shivalkar
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mohan,

I'm not sure if Oracle (which now owns SUN) signs the jars (I think so), but due to their security manager, it will also prevent you from writing your own class loader to load the system classes (correct me if I'm wrong).

Otherwise, a dirty hack would have been to either:
1) replace the class file in jar
2) write a new class loader, and while loading a particular class, read it from some other class file, or change the class after it is read (but before it is loaded)

Even if above approach is possible, I really don't think if it is legal.

As of now, the only option - as Campbell has suggested, is to write your own Java installation - which, you need to get certified from Oracle (which will certify only after confirming that your new JDK is compliant to Java specification and related JSR). For more info about this, you can read about TCK(<-click). And if you are doing it out of curiosity, in my opinion, the effort is not worth

However, if you want to check how you can change a behavior of jar and/or class, you can always write your own class and then change the behavior as you wish - as long as you do not use any security manager(s) in your class.
 
Mohan Gaddam
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anayonkar Shivalkar wrote: 1) replace the class file in jar


Hello Anayonkar,
good to see your reply, i did the same case as in tagged, it kicked me with a SecurityException

Thanks
Mohan
 
E Armitage
Rancher
Posts: 989
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mohan Gaddam wrote:
Jesper de Jong wrote:Welcome to the Ranch.

In case if i have to chaneg the collection classes without extending for performance, this might be the better way.

Unlikely. The alternative cleaner approaches would not perform significantly less than changing the core classes.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16057
88
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mohan Gaddam wrote:In case if i have to chaneg the collection classes without extending for performance, this might be the better way.

No, changing the standard collection classes because you think that would help performance is not a good idea at all.

Oracle creates the JVM and the standard classes using their own methods. They likely have thousands of automatic tests to test if everything works correctly. I've once read somewhere that testing and releasing a new Java version is a process that takes Oracle a few weeks to execute. They have to be absolutely sure that there are no big errors in the new version they release, because if there were millions of Java developers and users would have problems.

The standard collection classes have been written and have been tweaked, optimized and maintained for years by very smart software developers from Sun and Oracle. It's unlikely that you can change them to make them perform significantly better. And if you really do have a brilliant idea, then you should join the OpenJDK project and submit your code there. Who knows, if Oracle recognizes your contribution, it will end up in a future Java version some day.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Another option is to interpose the class. Classes in the bootstrap classpath are loaded (and hence, used) before the rt.jar classes.

Henry
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!