Power to Build

Home » Programming » Java

Category Archives: Java

Have you heard of CAFEBABE, DEADBEEF, DOCFILE0?


Have you heard of a magic word CAFE BABE? They are the magic numbers of the Java class files. See below link for the interesting story behind it:

https://dzone.com/articles/the-magic-word-in-java-cafebabe

And the words, CAFE and BABE both actually make up the below hexadecimal numbers:

(CAFEBABE)16 = (3405691582)10

If you wonder why a magical word is needed, it’s a long practice in identifying files. See the wiki postwiki post about this.

DEAD BEEF (actually an hexa decimal number) is the magic word used by Texas Instrument in MAC Addresses.

Oh, the other word in my title, DOCFILE0 is actually a pseudo word = D0CF11E0. That is magic word used in the header of Windows EXE files. See here for more on that.

Sybase EAServer: Taking Control – Changing JVM in Jaguar Manager


EA Server 5.5 comes with a manager application called Jaguar Manager (also called Sybase Central). This is really a Java product, but in this version it’s wrapped in a program called scjview.exe. This is available in %SYBASE%\Shared\Sybase Central 4.3\win32.

I found out this is using an old JRE 1.4. (I use process explorer in Sysinternals. It’s a great tool!). I wanted to switch the EA Server to Java 1.5 , which is the latest version of Java supported in EAS 5.5. With the EA Server itself, this is easy. Just pass -jdk15 to the serverstart batch file.

With Jaguar Manager, this is not as straightforward. There is a batch file called, jagmgr.bat, but this only calls scjview.exe mentioned above. The Java runtime is probably picked up using a DLL named jsyblib142.dll, in %SYBASE%\Shared\win32.

After googling for a bit, I found a solution to this problem. Apparently, scjview.exe has a command line argument -batch.

 

scjview -batch

When you run it like that, the program generates a batch file called sybasecentral.bat in the same directory as scjview.

Now, this batch file runs Java with a bunch of Jar files. You can change the path of the Java command to change the Java version it’s run in. Such a simple solution, completely hidden! And with this available, why did they have to create an EXE like that?? Beats me!

Of course, once you convert to batch file, you can tweak the java parameters such as memory etc to run better.


Java/Log4J: What in the world is FQCN?


If you have used Log4J for logging in Java, chances are you stumbled on FQCN and wondered what it was all about. Burt, in his blog post below, explains it nicely!

Burt Beckwith’s Grails, Groovy, and Java blog

via WTF is Log4j’s FQCN?.

Java: Pass by what?


I was talking to a co-developer last week during one of our lunch time walks(yes, we talk tech during out walks – even if our topic starts with something else, we always end up with tech stuff – once a geek, always a geek! :)). He is a C programmer and we often discuss Java vs C. One thing that came up last week was about Pointers. Having done both C and Java, I started explaining how Java doesn’t really have pointers, but internally it uses pointers. And that though Java has pointers (actually references), we won’t be able to change the address of a pointer like you would in C or C++.

In this context, we touched on parameter passing. I mentioned casually that Java passes the primitives by value and objects by reference (this seems to be common urban myth! Judging by the hits on this topic in Google, I think I am not the only one thinking this). I thought this would make sense, as it would be ridiculous for Java to pass around bulky objects, but instead the pointers (references) to the objects around. This makes sense, as you are able to change the values of attributes inside the object. (This was only possible in C/C++, if you passed the pointer/reference/address to an object/structure). Since we are passing around reference to the object, I said, it’s pass-by-reference when it came to objects, with the condition that “you cannot change those references”. This is where I slipped.

Apparently, those references (addresses) of those objects are passed around “by value” – meaning you cannot change them. And thus the argument that everything is pass-by-value in Java. Definitely got me. As the O’Reilly book describes it, “Java handles Objects and Arrays “by reference”, but when it comes to parameter passing, it is a Pass-by-value language. When a reference type is involved, the value that is passed is the reference!!”. Here is a nice discussion on Stackoverflow about this. There are several nice responses there that explain this in detail, so I won’t waste your time explaining the same thing here.

But Java is not alone in this – here is a nice discussion on the topic in Python. In the author’s words,

Unfortunately, Python is “pass-by-object-reference”, of which it is often said:

“Object references are passed by value.”

Incidentally, C is also a pass-by-value by default, but a programmer can achieve pass-by-reference there by passing pointers or pointer-to-pointers!! See here. It’s splitting hair at this point, but many languages are pass-by-value by default, but offer some mechanism to pass-by-reference. Even PHP employs a similar technique to pass by reference, as discussed here. Great wikipedia offers this on the topic:

Many languages support call-by-reference in some form or another, but comparatively few use it as a default, e.g. Perl. A few languages, such as C++, PHP, Visual Basic .NET, C# and REALbasic, default to call-by-value, but offer special syntax for call-by-reference parameters. C++ additionally offers call-by-reference-to-const.

There we go, the saving grace. At least, my understanding was not totally off, though I needed new terminology!! Moral of the story is “never assume anything” in IT.

Here are some more interesting links on the topic:

http://javadude.com/articles/passbyvalue.htm

http://programmers.stackexchange.com/questions/141834/how-is-a-java-reference-different-from-a-c-pointer

http://docstore.mik.ua/orelly/java-ent/jnut/ch02_10.htm

http://programmers.stackexchange.com/questions/153474/why-are-so-many-languages-passed-by-value

http://www-cs-students.stanford.edu/~sjac/c-to-cpp-info/references

http://robertheaton.com/2014/02/09/pythons-pass-by-object-reference-as-explained-by-philip-k-dick/

UML Doclet in Eclipse


I use Eclipse (Kepler) for my Java development. As you know, Eclipse is an Open Source IDE available for many languages/platforms. It’s great with many options even my PowerBuilder IDE doesn’t provide.

Where Eclipse is lacking, it allows for Plugins to be added. This is really great. Being Open Source, there are so many options available out there.

I wanted to generate UML for my Java classes and I tried various plugin in Eclipse. I did not want to hand code them, but instead just import Java classes and let the tool generate UML. They did not work as I wanted. Closest one I got was ObjectAid. This actually lets me create a class diagram and drag and drop Java classes. It generates decent class diagram. The only problem being it seems to only list Composition relationships and not inheritance.

Then I stumbled on YWorks UML Doclet. Essentially, you use this as a custom doclet in the Javadoc options and it generates Javadoc with embedded UML diagrams!!

Getting UML Doclet to work is a bit tricky. You have to mention several options, but I didn’t find one place that mentions all these options yet. After some trial and error and enough Googling, I found the following setup works for me:

In Eclipse, Click on Project -> Generate Javadoc. This will open the Javadoc Dialog (You can also use File -> Export -> Javadoc to get here).

If multiple projects open, choose the project you want Javadoc generated for. This is no different from generating the regular Javadoc. Now, if you want UML Doclet to do its job, choose Custom Doclet and enter the following details:

Doclet Name: ydoc.doclets.YStandard
Doclet Class Path: <YDoclet Dir>\lib\ydoc.jar

Where <YDoclet Dir> is the absolute path where installed/copied the YWorks Doclet to. Type the full path there. Since I am on Windows, I use backslashes and double up all backslashes for Java.

For e.g., I’ve installed my Doclet at C:\util\yworks-uml-doclet-3.0_02-jdk1.5\, so I replaced <YDoclet Dir> with this.

Click Next


Where it says VM Options, enter the below string:

-license <YDoclet Dir>\resources\ydoc.license -resourcepath <YDoclet Dir>\resources\ -docletpath <YDoclet Dir>\resources;<YDoclet Dir>\lib\class2svg.jar -umlautogen -d c:\temp\javadoc

So for example, in my case, I had to set the VM options to,

-license C:\\util\\yworks-uml-doclet-3.0_02-jdk1.5\\resources\\ydoc.license -resourcepath C:\\util\\yworks-uml-doclet-3.0_02-jdk1.5\\resources\\ -docletpath C:\\util\\yworks-uml-doclet-3.0_02-jdk1.5\\resources;C:\\util\\yworks-uml-doclet-3.0_02-jdk1.5\\lib\\class2svg.jar -umlautogen -d c:\\temp\\javadoc

Notice -d option to set the destination. I added this, so I could find my generated Javadoc files!! Otherwise, I couldn’t find the files after JavaDoc was generated (no it did not put it in Project folder, like regular Javadoc would do). -umlautogen probably adds the UML diagrams to the Javadoc.

(Also, note that the VM options text box doesn’t scoll in the version of Eclipse I have. If so, make the window itself wider. Or make the options string in notepad and paste it into the above box).

Choose JRE Source compatibility.

These settings will be retained in Javadoc generation, until you change it. You also have the option of saving these saving these settings in an Ant script, so you can run it outside of Eclipse as well.

Click Finish, voila!! You will see your Javadoc with embedded UML like shown below.


Update 11/09/2015

I saw a question on Stackoverflow that linked back to this post. Essentially, the post authors were getting below error message:

javadoc: error - In doclet class ydoc.doclets.YStandard,  method start has thrown  
an exception  
java.lang.reflect.InvocationTargetException java.lang.NoSuchMethodError:

com.sun.tools.doclets.internal.toolkit.taglets.TagletManager.  
getConstructorCustomTags()[Lcom/sun/tools/doclets/internal/toolkit/taglets/Taglet;
....

I did some testing on this and found out that they might be using Java JDK 1.8. My project uses Java 1.5, that’s why I downloaded the ydoc for Java 1.5 and set it to 1.5 compatibility above. But, just for this test, I changed the Javadoc Command on the first window to C:\Java\jre1,8.0_65\bin\Javadoc.exe. This caused the above error.

I tried mixing Javadoc.exe from Java 1.6 and Java 1.7 directories and they did fine. So, it seems to be a Javadoc version issue. Incidentally, yworks mentions only JDK 1.5, 1.6, 1.7 as supported version for the ydoc.

Java Stored Procedures


I am sure you are familiar with Stored Procedures. Almost every major database vendor supports it. They are program units that are precompiled and stored inside the database. Since the program is inside the database, it is tightly coupled with the database SQL language constructs. This and the precompiled nature of these programs, make the database access faster. So, even if you are writing external programs that access the database, you will benefit from coding and combining the database operations in stored procedures. This saves a lot of back and forth and thus network traffic. Since version 7, Oracle has support for Stored Procedures. These programs are coded a in native language called PL/SQL.

Since Oracle 8i, Oracle supports coding and running similar procedures (program units) written in Java. Like PL/SQL procedures, these programs are precompiled and stored inside the database and are known as Java Stored Procedures. The JAVASPs(1) are stored as classes (in blob fields) and when invoked they are run inside a JVM that runs within Oracle database. Prior to Oracle 10g, they needed to be wrapped inside a PL/SQL procedure or package. Since Oracle 10g, you can actually invoke Java classes directly from SQL (just the same way you call a PL/SQL function in a SQL).

JavaSPs are different from regular java programs in that they actually run inside a VM within the database. There are a few restrictions while coding a JAVASP.

Building and Deploying Java SP

To write and build the Java stored procedures, you can use your standard Java development environment. I use eclipse to develop and Ant to build it

build_LATAXSP.xml has steps to compile java classes and build jar file.

Deploying Java SP

At this point the jar file is ready to be deployed to Oracle. Typically we pass this step onto the DBA who will then load the jar into Oracle Database instance. But during development, developer can load these themselves using the loadjava utility. This is typically available on the machine where the database is running. (Remember, it is run by DBAs?). In our case we have the Oracle databases running on Unix boxes, so we have loadjava utility available there. I upload the file to Unix and run loadjava. While uploading make sure it’s in binary mode.

Below screenshot shows a sample run of loadjava command on Unix.

loadjava_results

In this example, I loaded all the classes in a jar file, to the database. As shown there were 24 classes and 2 resources loaded and there were no errors. If the command failed to load the Java classes, you will see an error message here. The first time around, all the classes files are loaded. Next successive load of the same jar file, will load only classes that have been modified since the last load.

Verifying the load

To view the objects in Oracle, following SQLs can be used:

select * from all_objects where object_type = ‘JAVA CLASS’ and owner = <owner>;

or

select * from user_objects where object_type = ‘JAVA CLASS’;

To see a little more detail about the Java stored procs, use

SELECT * FROM user_java_classes; — this lists java procs for the user.

Earlier I posted about displaying contents of the resources files (text files) loaded above.

Notes:
(1) Oracle actually refers to Java Stored Procedure as JSP. To avoid confusion with Java Server Pages, I prefer JavaSP.

Quick Tip: Java Stored Procedures in Oracle – Reading Content


This post is about how to get the contents of a resource file that is loaded into Oracle as part of Java Stored Procedures package. I’ve recently posted about Java Stored Procedures in general here.

While working on enhancing some Java procedures, I added a simple Java logger class, fashioned after Log4J. To be able to dynamically configure the logging properties (log levels, threshold), I added in a properties file, bundled into the Jar file itself1. After loading the properties file into the Database (using LoadJava command; loaded as a Java Resource), I wanted a way to “see” what’s inside the file. That’s when I posted a question on StackOverflow, but eventually googled and found the answer. See my question and my own answer here. And here is the script, I came up with:

SET SERVEROUTPUT ON
EXEC DBMS_JAVA.SET_OUTPUT (1000000);
DECLARE
   bText CLOB; 
   len PLS_INTEGER;
   offset PLS_INTEGER;
   text VARCHAR2(2000);
BEGIN
   DBMS_LOB.CreateTemporary(bText, FALSE );
   DBMS_JAVA.Export_Resource('LAJavaSP.properties', bText);
   len := 1000; -- length of the lob to read.
   offset := 1;
   DBMS_LOB.Read(bText, len, offset, text); 
   DBMS_OUTPUT.Put_Line(text);
END;
/

This printed the following:

logLevel=ERROR
dbLogLevel=ERROR
For now, that’s all I have in the properties file. Hoping to add more later.

1 Adding the properties file into the jar file makes it easy to read it using getSystemResourceAsStream. Otherwise, we will have to worry about Directory objects, Permissions etc.