Creating a runnable jar. Various problems

I know responding to your question can be frowned upon, but I did manage to solve this problem yesterday. Of all the options I tried I found the fastest solution was to replicate the Eclipse Runnable Jar Export Wizard. It requires the jar-in-jar-loader.zip which can be acquired using the Runnable Jar Export Wizard… or you can find it via Google/Eclipse install.

I found this solution was fast to build (30s) and much faster to run (5s bootup cost). It also left the jar structure very neat with no exploded jars.

<target name="compile" depends="resolve">
    <mkdir dir="bin"/>
    <!-- Copy all non java resources since jar javac will exclude them by default. Needed for xmls, properties etc --->
    <copy todir="bin">
        <fileset dir="src" excludes="**/*.java" />
    </copy>

    <javac srcdir="src" destdir="bin" debug="true" deprecation="on">
        <classpath>
            <path refid="ivy.path" />
        </classpath>
    </javac>
</target>


<!-- Creates the runnable jar. Copies the dependencies as jar files, into the top level of a new jar. 
     This means nothing without a custom classloader and manifest with a jar listing -->
<target name="jar" depends="compile" description="Create one big jarfile.">

    <path id="dependencies.path">
        <fileset dir="lib">
            <include name="**/*.jar" />
        </fileset>
    </path>

    <pathconvert property="manifest.classpath" pathsep=" ">
        <path refid="dependencies.path" />
        <mapper>
            <chainedmapper>
                <flattenmapper />
                <globmapper from="*.jar" to="*.jar" />
            </chainedmapper>
        </mapper>
    </pathconvert>

    <mkdir dir="${dist}"/>
    <jar destfile="${dist}/jar/Runnable.jar" filesetmanifest="mergewithoutmain">
        <manifest>
            <attribute name="Main-Class" value="org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader" />
            <attribute name="Rsrc-Main-Class" value="mymainclass" />
            <attribute name="Class-Path" value="." />
            <attribute name="Rsrc-Class-Path" value="./ ${manifest.classpath}" />
        </manifest>
        <fileset dir="bin" />
        <zipfileset src="jar-in-jar-loader.zip" />
        <zipfileset dir="lib" includes="**/*.jar*" />
    </jar>
</target>

http://stackoverflow.com/questions/10238330/creating-a-runnable-jar-various-problems

What’s the best way to implement a Request/Reply pattern if no temporary queues are available?

The customary solution when several requestors listen for replies on the same queue is to use correlation IDs to select messages. On the client side it looks like this:

Place a message on the request queue and commit.
Retrieve the JMSMessageID from the outbound message (the value is determined by the broker and updates the message object as a result of the send).
Receive a message from the reply queue specifying the JMSMessageID from the outbound message as the correlation ID in the selector.
Process and commit.
On the server side it looks like this:

Receive a message under syncpoint.
Process the request and prepare the response.
Set the JMSCorrelationID on the response to the value of JMSMessageID from the request.
Send the message.
Commit.
The consumer would set the selector something like this: activemq.selector:JMSCorrelationID=.

Since the broker creates a message ID that is supposed to be globally unique, the pattern of using it as the correlation ID prevents collisions that are possible when each requestor is allowed to specify it’s own value.

http://stackoverflow.com/questions/4849964/whats-the-best-way-to-implement-a-request-reply-pattern-if-no-temporary-queues#

What is the difference between call and apply?

The difference is that apply lets you invoke the function with arguments as an array; callrequires the parameters be listed explicitly. A useful mnemonic is “A for array and C for comma.”

See MDN’s documentation on apply and call.

Pseudo syntax:

theFunction.apply(valueForThis, arrayOfArgs)

theFunction.call(valueForThis, arg1, arg2, ...)

Sample code:

function theFunction(name, profession) {
    alert("My name is " + name + " and I am a " + profession + ".");
}
theFunction("John", "fireman");
theFunction.apply(undefined, ["Susan", "school teacher"]);
theFunction.call(undefined, "Claude", "mathematician");

http://stackoverflow.com/questions/1986896/what-is-the-difference-between-call-and-apply

Examples of Algorithms which has O(1), O(n log n) and O(log n) complexities

If you want examples of Algorithms/Group of Statements with Time complexity as given in the question, here is a small list –

O(1) time
1. Accessing Array Index (int a = ARR[5];)
2. Inserting a node in Linked List
3. Pushing and Poping on Stack
4. Insertion and Removal from Queue
5. Finding out the parent or left/right child of a node in a tree stored in Array
6. Jumping to Next/Previous element in Doubly Linked List
and you can find a million more such examples…

O(n) time
1. Traversing an array
2. Traversing a linked list
3. Linear Search
4. Deletion of a specific element in a Linked List (Not sorted)
5. Comparing two strings
6. Checking for Palindrome
7. Counting/Bucket Sort
and here too you can find a million more such examples….
In a nutshell, all Brute Force Algorithms, or Noob ones which require linearity, are based on O(n) time complexity

O(log n) time
1. Binary Search
2. Finding largest/smallest number in a binary search tree
3. Certain Divide and Conquer Algorithms based on Linear functionality
4. Calculating Fibonacci Numbers – Best Method
The basic premise here is NOT using the complete data, and reducing the problem size with every iteration

O(nlogn) time
1. Merge Sort
2. Heap Sort
3. Quick Sort
4. Certain Divide and Conquer Algorithms based on optimizing O(n^2) algorithms
The factor of ‘log n’ is introduced by bringing into consideration Divide and Conquer. Some of these algorithms are the best optimized ones and used frequently.

O(n^2) time
1. Bubble Sort
2. Insertion Sort
3. Selection Sort
4. Traversing a simple 2D array
These ones are supposed to be the less efficient algorithms if their O(nlogn) counterparts are present. The general application may be Brute Force here.

I hope this answers your question well. If users have more examples to add, I will be more than happy 🙂

http://stackoverflow.com/questions/1592649/examples-of-algorithms-which-has-o1-on-log-n-and-olog-n-complexities

Determining if an Algorithm is O (log n)

I know with O(n), you usually have a single loop; O(n^2) is a double loop; O(n^3) is a triple loop, etc. How about O (log n)?

You’re really going about it the wrong way here. You’re trying to memorize which big-O expression goes with a given algorithmic structure, but you should really just count up the number of operations that the algorithm requires and compare that to the size of the input. An algorithm that loops over its entire input has O(n) performance because it runs the loop n times, not because it has a single loop. Here’s a single loop with O(log n) performance:

for (i = 0; i < log2(input.count); i++) {
    doSomething(...);
}

So, any algorithm where the number of required operations is on the order of the logarithm of the size of the input is O(log n). The important thing that big-O analysis tells you is how the execution time of an algorithm changes relative to the size of the input: if you double the size of the input, does the algorithm take 1 more step (O(log n)), twice as many steps (O(n)), four times as many steps (O(n^2)), etc.

Does it help to know from experience that algorithms that repeatedly partition their input typically have ‘log n’ as a component of their performance? Sure. But don’t look for the partitioning and jump to the conclusion that the algorithm’s performance is O(log n) — it might be something like O(n log n), which is quite different.

http://programmers.stackexchange.com/questions/146021/determining-if-an-algorithm-is-o-log-n

Oracle BLOB to File Extract…

If you are using TOAD, then you don’t need to look any further to export BLOB column data into a file.
Just write a simple select statement (select blob_column, reg_col as file_name from blob_table;) in Editor and execute the statement. Right Click in the Data Grid Once the records are fetched and chose option “Export Blobs” and follow the instructions you are good to go.

Well, depending on the resources available on your PC, TOAD might throw an error with Low Memory or other issues when the exporting BLOB entries are huge or number of records to fetch are more.

Here is the simple proc that can be utilized instead and store all these files on the database server and get’em later.
Make sure that you have write access to Oracle Directory that you are going to use.

CREATE OR REPLACE procedure blob2file
is
t_blob   blob;
vstart  number := 1;
bytelen number := 32000;
t_blob_len     number;
my_vr   raw(32000);
x       number;
t_file_name varchar2(200);
l_output utl_file.file_type;
begin

for i in (
select reg_col file_name,
dbms_lob.getlength(blob_column) blob_len,
blob_column blob_val
from blob_table
) loop

begin
— save blob length
x := i.blob_len;

— define output directory
l_output := utl_file.fopen(‘ORACLE_DIRECTORY_NAME_HERE‘, i.file_name, ‘WB’, 32760);

— if small enough for a single write
if i.blob_len < 32760 then
utl_file.put_raw(l_output,i.blob_val);
utl_file.fflush(l_output);
else — write in pieces
vstart := 1;
while vstart < i.blob_len
loop
dbms_lob.read(i.blob_val,bytelen,vstart,my_vr);

utl_file.put_raw(l_output,my_vr);
utl_file.fflush(l_output);

— set the start position for the next cut
vstart := vstart + bytelen;

— set the end position if less than 32000 bytes
x := x – bytelen;
if x < 32000 then
bytelen := x;
end if;
end loop;
end if;
utl_file.fclose(l_output);

end;

end loop;

end blob2file;
/

http://sanoralife.blogspot.com/2012/03/oracle-blob-to-file-extract.html

Correlation between Java EE / J2EE to J2SE / JDK versions

Is there an official correlation between Java EE / J2EE and J2SE / JDK versions?

Sun may try to keep some kind of “correlation” to not confuse users and for marketing purpose but, AFAIK, no, there is no official correlation (and maintaining the pseudo synchronisation might become harder in the future as the JDK evolves faster than the Java EE specification).

UPDATE: I was wrong, there is actually a correlation (see below). But I still think that this might become a problem: Java 7 is scheduled for late 2010, will the expert group succeed to roll out Java EE 7 before Java 8 and Java 7 EOL?

If the answer is yes, where is it written?

Quoting the JSR 151: JavaTM 2 Platform, Enterprise Edition 1.4 (J2EE 1.4) Specification home page:

  • J2EE 1.4 is the Enterprise Edition of version 1.4 of the Java platform, and thus will be built on J2SE 1.4.

Quoting the JSR 244: Java(TM) Platform, Enterprise Edition 5 (Java EE 5) Specification home page:

  • Java EE 5 is the Enterprise Edition of version 5 of the Java platform, and thus will be built on J2SE 5.0.

Quoting the JSR 316: JavaTM Platform, Enterprise Edition 6 (Java EE 6) Specification home page:

  • Java EE 6 is the Enterprise Edition of version 6 of the Java platform, and thus will be built on Java SE 6.

Seriously, why do we have to go to the JCP website to find this (maybe I should have started there though)?

If the answer is no, then why?

Java Enterprise Edition is a specification, not an implementation. It might rely on specific features (like Java EE 5 and Java 5 generics) but the JDK version is actually dependent upon the implementation of the container you are using. See the Websphere example below. (This still applies, even after the big UPDATE above.)

and is there an unofficial version table (e.g. JDK 1.4 goes well with J2EE 1.3, JDK 1.5 goes well with J2EE 1.4, JDK 1.6 with Java EE 5 and so on)

As I said above, “goes well” doesn’t really makes sense as this depends on the container you are using and containers might have different compatibility requirements (for example IBM WebSphere’s J2EE 1.4 server may not work with JDK 5). So, I’d rather speak in terms of requirements.

The J2EE 1.4 SDK is bundled with JDK 5 but… J2EE 1.4 requires JDK 1.4 or later. Java EE 5 requires JDK 5 or later. Java EE 6 requires JDK 6 or later (JDK 5 is at its EOL for 2+ months now).

http://stackoverflow.com/questions/2013958/correlation-between-java-ee-j2ee-to-j2se-jdk-versions

What exactly is a Context in Java?

In programming terms, it’s the larger surrounding part which can have any influence on the behaviour of the current unit of work. E.g. the running environment used, the environment variables, instance variables, local variables, state of other classes, state of the current environment, etcetera.

In some API’s you see this name back in an interface/class, e.g. Servlet’s ServletContext, JSF’s FacesContext, Spring’s ApplicationContext, Android’s Context, JNDI’s InitialContext, etc. They all often follow the Facade Pattern which abstracts the environmental details the enduser doesn’t need to know about away in a single interface/class.

http://stackoverflow.com/questions/3918083/what-exactly-is-a-context-in-java