Share your thoughts and find that its getting better every day. This work of mine helps me realize that.
Friday, February 13, 2015
Got Crazy with nested classes :)
Step 1: created a class like the following
package com.oz.core.NestedClasses;
public class OuterClass {
public static class NestedClass {
public class SubNestedClass {
public class Level4 {
public class Level5 {
public class Level6 {
public class Level7 {
public class Level8 {
public class Level9 {
public class Level10 {
public class Level11 {
public class Level12 {
public class Level13 {
public class Level14 {
public class Level15 {
public class Level16 {
public class Level17 {
public class Level18 {
public class Level19 {
public class Level20 {
public class Level21 {
public class Level22 {
public class Level {
Level() {
System.out
.println("Road To Eldorado WHOOO...HOOOO");
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
Step 2: instantiated the classes Lol
package com.oz.core.NestedClasses;
import com.oz.core.NestedClasses.OuterClass.NestedClass.SubNestedClass.Level4.Level5;
public class Main {
public static void main(String args[]) {
OuterClass outCls = new OuterClass();
OuterClass.NestedClass nesCls=new OuterClass.NestedClass();
OuterClass.NestedClass.SubNestedClass subNestedClass=
nesCls.new SubNestedClass();
OuterClass.NestedClass.SubNestedClass subNestedClass2=
new OuterClass.NestedClass().new SubNestedClass();
OuterClass.NestedClass.SubNestedClass.Level4 lvl4= subNestedClass2. new Level4();
OuterClass.NestedClass.SubNestedClass.Level4.Level5.Level6.Level7.Level8.Level9.Level10
.Level11.Level12.Level13.Level14.Level15.Level16
.Level17.Level18.Level19.Level20.Level21.Level22
.Level level = lvl4.new Level5().new Level6().new Level7().new Level8().new Level9().new Level10()
.new Level11().new Level12().new Level13().new Level14().new Level15().new Level16().new Level17().new Level18()
.new Level19().new Level20().new Level21().new Level22().new Level();
}
}
Step 3: ran the main program to see the output on the console ☺
Console Output:
Road To Eldorado WHOOO...HOOOO
Wednesday, November 5, 2014
File Upload using Servlets
<servlet-name>UploadServlet</servlet-name>
<servlet-class>com.practise.karthik.servlet.UploadServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>UploadServlet</servlet-name>
<url-pattern>/UploadServlet</url-pattern>
</servlet-mapping>
Step 6: Now stop your Apache Tomcat Server if its already running.
- Right click on the html file mentioned in step 1,
- click on "Run as" and
- click on "Run on server" option
- choose your installed Tomcat server ( hope you had already added the server to your Servers view in the eclipse) and
- finish
This helped me get to the following screen
Choose your file and click on the "Upload File" button. You should receive a confirmation screen with the name of the file that you just uploaded as follows
This helped me upload my file to my server space configured in the web.xml and I am able to find the uploaded file as follows
Thursday, August 14, 2014
error c00c0240 - "Could not complete the operation due to error c00c0240." - AJAX request in Struts
I happened to encounter this error when I was attempting to debug an Ajax call to a Struts action. It made me feel miserable for a while as both firebug as well as eclipse debuggers with console showed no trace of the origins of the error. As usual I searched out for the error number and ended up with a lot of stack overflow posts. One of these posts were typically explaining me the same issue that I was facing and had a link to a blog post which contained a solution to the same. And to my relief, my companies network restrictions prevented that page from being loaded. Wait WTF!!! Yes... that’s how grateful I felt roflJ.
Enough of my story, the issue in my case was that the AJAX request that I framed and tested was
var x= new XMLHttpRequest();x.open("GET","http://localhost:7001/****/*************Action.do",false);x.send(null);x.responseText
This was giving me some un relevant message like the following and my debugging points never worked and neither the eclipse console had any logs printed in them through the log4j framework.
"
<html>
<head>
<script type="text/javascript" src="/****/js/*****.js"></script>
<script type="text/JavaScript">
window.document.onkeydown=checkKeyCode;
</script>
</head>
<body>
<table width="100%" valign="top" height="100%">
<tr>
<td align="center"><font color="red"><h4>
There was a problem, please try again!</h4></font>
</td>
</tr>
<tr>
<td align="center"> </td>
</tr>
</table>
</body>
</html>"
And I found the “THE ISSAC NEWTON” in me like most of the times, as something stuck me and I tried “POST” instead of “GET”
And now, I see some positive results. My debugging points in the eclipse is working.. Hurray!!!!!
var x= new XMLHttpRequest();x.open("POST","http://localhost:7001/****/*************Action.do",false);x.send(null);x.responseText
Hurray!!!!! Again. But wait, WTF its just that my debugging points in eclipse are working. So wait my work is not done yet
L J
Friday, July 25, 2014
Writting run time code in eclipse for debugging as in firebug console
While debugging an application/program in eclipse , go to the display screen in the side menu items with in the eclipse and type run time variables and execute the same like you would prefer to do in firebug, where you type commands and click on run.
Alternatively, to bring up the Display screen, you can go to
window->show view ->Display or Ctrl+Shift+D
I have simply heard about the same and have not tried it yet. So plz don’t blame me if it does not work for you.
Hope this works 0.0
Thursday, June 5, 2014
getXMLString from an XML Node Object
Following is a simple function that would let you obtain the XML contents of a XML node in a string format. The packages that had been imported into the class are listed as follows. I hope these are available as a free download
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public static String getXMLString(Node xmlNode){
String xmlString = null;
xmlString="<";
xmlString+=xmlNode.getNodeName();
NamedNodeMap attributes=xmlNode.getAttributes();
int noOfAttributes=attributes.getLength();
int itr=0;
for (itr=0;itr<noOfAttributes;itr++) {
Node attributeNode=attributes.item(itr);
xmlString+=" "+ attributeNode.getNodeName() + "=" + attributeNode.getNodeValue();
}
xmlString+=">";
NodeList childNodes=xmlNode.getChildNodes();
int noOfChildNodes=childNodes.getLength();
if (noOfChildNodes==0) {
xmlString+=xmlNode.getNodeValue();
} else {
int childNodeItr=0;
for (childNodeItr=0;childNodeItr<noOfChildNodes;childNodeItr++) {
xmlString += getXMLString(childNodes.item(childNodeItr));
}
}
xmlString+="</"+xmlNode.getNodeName()+">";
return xmlString;
}
Creating an XML Document node from XML string
Following method helps you convert XML String into a XML Document Object
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
public Document getXMLDoc(String xmlString) {
InputStream is = new ByteArrayInputStream(xmlString.getBytes());
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = null;
Document doc = null;
try {
dBuilder = dbFactory.newDocumentBuilder();
doc = dBuilder.parse(is);
} catch (Exception e) {
// TODO Auto-generated catch block
// logger.error("Parsing exception iha xml traversal");
}
return doc;
}
You can use the doc object as follows
doc.getElementsByTagName(tagName)
Monday, June 2, 2014
Creating a jar file through eclipse
Its rather simple and straight forward to create a jar file using eclipse. I am employing an ant build file to do this stuff.
Create a project and add a class to it as follows
package com.self.jarTest;
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
Now create a build.xml file in the root of your project folder with the following content
<project>
<target name="clean">
<delete dir="build"/>
</target>
<target name="compile">
<mkdir dir="build/classes"/>
<javac srcdir="src" destdir="build/classes"/>
</target>
<target name="jar">
<mkdir dir="build/jar"/>
<jar destfile="build/jar/HelloWorld.jar" basedir="build/classes">
<manifest>
<attribute name="Main-Class" value="com.self.jarTest.HelloWorld"/>
</manifest>
</jar>
</target>
<target name="run">
<java jar="build/jar/HelloWorld.jar" fork="true"/>
</target>
</project>
Now right click on the build.xml and click on “Run As -> 3.Ant Build -> Clean, Compile, run, jar -> Run“
That’s it you got your jar file J
Creating a jar file through command prompt
It gives me a feeling like I am interacting more with my computer than usual. I feel like those so called geek hackers in Action movies as I don’t see anything visually until I clean, compile and run my jar files lol. I know I am exaggerating stuffs here, but, its’ sometimes good to enjoy the little things in life you know J
Coming to the point, In order to create a file entirely through command prompt, one can possibly follow the forth coming steps to do it.
1. Get to your project folder. In my case it was through
>>cd “projectFolderPath”
2. Create your class file. I created one to print a statement as follows
package com.self.jarTest;
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
3. Create a build directory
>>md build\classes
4. Psst. I don’t have admin rights on this stupid machine and so I can’t change my environment variables. So I did the following
>>"C:\program files\Java\jdk1.7.0_55\bin\javac.exe” –sourcepath src –d build\classes src\com\self\jarTest\HelloWorld.java
In your case, if your environment variables are set properly I assume it must be something similar to
>>javac –sourcepath src –d build\classes (path to your HelloWorld.java file)
5. Now create a folder to put your jar file
>>md build\jar
6. And finally create your jar file (* following line does include a ‘dot’ – ‘.’ – don’t omit it)
>>jar cfm build\jar\HelloWorld.jar mf –C build\classes .
@*%%*, My class path is not set properly and I can do nothing about it L So I did the following
>>"C:\program files\Java\jdk1.7.0_55\bin\jar.exe" cfm build\jar\HelloWorld.jar mf –C build\classes .
Vola, my jar file is created. Now to do the most meaning full stuff, I am speaking about the “Check if its working or not” theorem. I used the following line of command
>>java -jar "build\jar\HelloWorld.jar"
Wohooo…. It worked like charm. I got the following
>>Hello World
I know it seems like, I am getting exited for nothing. Who gives a damn J
Trouble shooting java.lang.UnsupportedClassVersionError
I encountered an error which stated ‘Exception in thread "main" java.lang.UnsupportedClassVersionError: …. Blah blah blah’ when I tried to run an class as a normal java application in eclipse. The code was fairly simple and hence I definitely had nothing to debug for errors. A little search revealed about the nature of the error and its fix which was again fairly simple and straight forward.
I used a default JRE which was included from JDK160_29 corresponding to a weblogic 12c server. But my compiler was set to Java 1.7
So all I had to do was change it to 1.6 and my code worked like charm J
So in summary the fix to my issue was File->Preferences->Java Compiler->’Compiler Compliance Level’ -> choose between 1.3 to 1.7 to suit your JRE version. In my case it was 1.6
Friday, May 30, 2014
Switch Case using String in Java
Switch case in Java using string values is a little tricky. The concept seems so simple when we think of a logic, only to surprise you later that it is not so simple and it seems like an impossible stuff. It has happened to me multiple times during different time periods with fairly distant gaps that I tend to forget this experience only to be stuck with it again and remember that this has happened to me before like a De-ja-vu.
I knew that that switch statements accepts integers, characters and enum types as input parameters. My exploration and try outs with enumeration helped me come up with an idea to use string values in Switch-case statement in a slight indirect way.
Following was my attempt and I am happy that I did it J
public class EnumTest {
public static void main(String args[]) {
String testName = "PERSON_B";
NameConst name = NameConst.valueOf(testName.toUpperCase());
switch (name) {
case PERSON_A:
System.out.println("Big Brother");
break;
case PERSON_B:
System.out.println("Small Brother");
break;
default: // do nothing or your default logic
}
}
}
enum NameConst {
PERSON_A("Person_A"), PERSON_B("PERSON_B");
private String nameValue;
private NameConst(String nameValue) {
this.nameValue = nameValue;
}
}
I obtained the following output in my console and am more than happy to see this work J
>>Small Brother



