Tuesday, 14 April 2015

Java Runtime Exception: Unsupported major.minor version 52.0

For this post, I'm going to talk about java related error in Eclipse when we switch jre or jdk from 1.7 to 1.8.

Enviroment : 
i. OS: Windows
ii. IDE: Eclipse Luna

Exception Message:
Exception in thread "main" java.lang.UnsupportedClassVersionError: xxxxx/Main : Unsupported major.minor version 52.0
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:791)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:482)


The root cause of this problem is due to the creation of the java project is in IDE with Java 8 (jre8 or jdk1.8). But the project maybe later imported into another IDE with Java 7 (jre7 or jdk1.7). This cause the mismatch of the java compiler version.

In order to solve this. Several things need to be in place.
  1. Ensure you have Eclipse support Java 8. I know Eclipse Luna and above support Java 8. I'm not sure about the other Eclipse. You to find out.
  2. Ensure you have install jdk1.8 or jre8 in your PC. You can download from Oracle
  3. If you're not sure about whether you have installed before, you can run the javac command in your console.
     
  4. You can also check the Environment variable of your Java_Home by right click on [Computer]->[Advanced system settings]->[Advanced] [Environment Variables]. Check the JAVA_HOME
  5. Make sure you path your JAVA_HOME.
  6. Now switch back to your Eclipse IDE.
  7. Goto [Windows]->[Preferences]->[Java]->[Compiler]. Ensure you select the JDK1.8 and also checked on the [Installed JREs] 
  8. If you're running Maven project. Ensure you configure your Maven to built for JDK1.8/JRE8.
  9. NOTE: If you're running standalone application, ensure your [RUN CONFIGURATIONS]->[JRE] is using the correct Runtime JRE. I realized that the it won't changed automatically eventhough we have insalled the JRE8.
  10. Hope either one of the steps help you solving your Unsupported major.minor version 52.0 exception.


Thursday, 15 January 2015

How to reset HTML file input control

HTML file input control is commonly use to upload files. The sample control looked like this.


Equivalent sample HTML:

<input type="file" file-model="myFile" id = "uploadCaptureInputFile" required />
<span>class="help-block">Max file size 2Mb</span>

There are few ways to reset the HTML file input control.

1. Using Javascript

Make sure you assign the id to your control. At the code that trigger reset, reset the value.
document.getElementById("uploadCaptureInputFile").value = "";           

If you're using jQuery, the equivalent object id will be $('#uploadCaptureInputFile').value = "";

2. Using Form Reset

Make sure your input control is placed in the same form tag. You can create a button type="reset". Upon clicking of the reset button, the input control will be reset as well. See these example & example2

The HTML Code is quite simple.
<button type="reset" value="Reset">Reset</button>

Method 2 are more rigid as it will reset all inputs within the form. Hope it helps.

Thursday, 15 May 2014

Creating C/C++ Dynamic Link Library (C/C++ DLL) Using Visual Studio

I was working on a project to create a C dynamic link library (.DLL). It seems difficult at the beginning. However, after several attempts, manage to produce it.

For creation of C++ library you can refer to the MSDN. I'm following these steps and enhance on the same codes to produce C DLL.

  1. In order to access a library file, we can utilize the Dependency Walker.
  2. The library that generated by the sample code by MSDN is as picture below. Take note of the difference between C++ functions and C functions.
  3. To export C functions, we can the export function names in .def file
  4. Header file will look something like this. The function for the Add is normal declaration in Header file.

  5. The implementation code is a standard code.
    1. #include "stdafx.h"

    2. #include "CryptoCPPDLL.h"
    3. #include <stdexcept>

    4. using namespace std;

    5. namespace CryptoFuncs
    6. {
    7.     double CryptoCPPDLL::Add(double a, double b)
    8.     {
    9.         return a + b;
    10.     }

    11.     double CryptoCPPDLL::Subtract(double a, double b)
    12.     {
    13.         return a - b;
    14.     }

    15.     double CryptoCPPDLL::Multiply(double a, double b)
    16.     {
    17.         return a * b;
    18.     }

    19.     double CryptoCPPDLL::Divide(double a, double b)
    20.     {
    21.         if (b == 0)
    22.         {
    23.             throw invalid_argument("b cannot be zero!");
    24.         }

    25.         return a / b;
    26.     }

    27. double CryptoCPPDLL::Add2(double a, double b)
    28.     {
    29.         return a + b;
    30.     }

    31.     double CryptoCPPDLL::Subtract2(double a, double b)
    32.     {
    33.         return a - b;
    34.     }

    35.     double CryptoCPPDLL::Multiply2(double a, double b)
    36.     {
    37.         return a * b;
    38.     }

    39.     double CryptoCPPDLL::Divide2(double a, double b)
    40.     {
    41.         if (b == 0)
    42.         {
    43.             throw invalid_argument("b cannot be zero!");
    44.         }

    45.         return a / b;
    46.     }
    47. void CryptoCPPDLL::PassStructIn(MyStruct* myStruct)
    48. {
    49.       if (myStruct != NULL)
    50.       {

    51.             myStruct->SomeId = 234;
    52.             myStruct->SomePrice = 456.23;
    53.       }
    54. }
    55. void CryptoCPPDLL::PassStructIn2(MyStruct* myStruct)
    56. {
    57.       if (myStruct != NULL)
    58.       {

    59.             myStruct->SomeId = 234;
    60.             myStruct->SomePrice = 456.23;
    61.       }
    62. }

    63. void PassStructIn3(MyStruct* myStruct)
    64. {
    65.       if (myStruct != NULL)
    66.       {

    67.             myStruct->SomeId = 234;
    68.             myStruct->SomePrice = 456.23;
    69.       }
    70. }

    71. double Add3(double a, double b)
    72. {
    73. return a + b;
    74. }

    75. }
  6. The second way to do this is through extern "C" __declspec(dllexport) YOUR_FUNCTION_NAME.
  7. Hope this helps. I will create another post to how to do it in Eclipse using GCC.







Tuesday, 13 May 2014

How To Check Build Status In TFS (Team Foundation Server) From Visual Studio IDE 2012

If your organization is using TFS as source control, build & deployment agent, and you are required to check in codes that you have written, it is important for you to ensure that the codes you have checked in compiled and build successfully by TFS Build Controller.

It's pretty simple steps to check it.

** I assume your Visual Studio IDE already configured to connect to the TFS server in your organization.

2.



Tuesday, 15 April 2014

RangeValidator Control Date Checking Error


Server Error in '/' Application.


 

The value '15/04/2024' of the MaximumValue property of 'RangeValidator2' cannot be converted to type 'Date'.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 


[HttpException (0x80004005): The value '15/04/2024' of the MaximumValue property of 'RangeValidator2' cannot be converted to type 'Date'.]
   System.Web.UI.WebControls.RangeValidator.ValidateValues() +1482938
   System.Web.UI.WebControls.RangeValidator.ControlPropertiesValid() +12
   System.Web.UI.WebControls.BaseValidator.OnPreRender(EventArgs e) +36
   System.Web.UI.Control.PreRenderRecursiveInternal() +83
   System.Web.UI.Control.PreRenderRecursiveInternal() +155
   System.Web.UI.Control.PreRenderRecursiveInternal() +155
   System.Web.UI.Control.PreRenderRecursiveInternal() +155
   System.Web.UI.Control.PreRenderRecursiveInternal() +155
   System.Web.UI.Control.PreRenderRecursiveInternal() +155
   System.Web.UI.Control.PreRenderRecursiveInternal() +155
   System.Web.UI.Control.PreRenderRecursiveInternal() +155
   System.Web.UI.Control.PreRenderRecursiveInternal() +155
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +974
 
I have encountered this problem when try to use range validator control.
The reason is because the server date locale setting is "mm-dd-yyyy" (en-US) format. But I'm trying to assign "wrong" value "15/04/2004" (which in my application, i'm using dd/mm/yyyy format across all my date value) into the range validator maximum value.
To overcome this problem, these are what i do.