Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts

2019-06-27

Language performance test

Lower value shows better performance

  • C
    #include <stdio.h>
    
    int fib(int n) {
        if(n == 1) return 0;
        if(n == 2) return 1;
        return fib(n - 2) + fib(n - 1);
    }
    
    int main() {
        int i = 0;
        for(i = 1; i <= 40; i++) {
            printf("%d %d\n", i, fib(i));
        }
        printf("\nC\n");
        return 0;
    }
    
  • Java
    public class Fib {
     
        public static int calc(int n) {
            if(n <= 1) return 0;
            if(n == 2) return 1;
            return Fib.calc(n - 2) + Fib.calc(n - 1);
        }
        public static void main(String[] args) {
            for(int i = 1; i <= 40; i++) {
                System.out.println(i + " " + Fib.calc(i));
            }
            System.out.println("\nJAVA");
        }
     
    }
    
  • JavaScript
    #!/usr/bin/env node
    
    let range = Array.from({length: 40}, (x, i) => i+1);
    let fib = n => n === 1 ? 0 : n === 2 ? 1 : fib(n - 2) + fib(n - 1);
    
    range.forEach( i => console.log(i, fib(i)) );
    console.log("\nJavaScript");
    
  • PHP
    #!/usr/bin/env php
    <?php
    function fib(int $n) {
        if ($n === 1) return 0;
        if ($n === 2) return 1;
        return fib($n - 2) + fib($n - 1);
    }
    
    foreach (range(1, 40) as $i) {
        echo $i, ' ', fib($i), PHP_EOL;
    }
    echo "\nPHP\n";
    
  • Python
    #!/usr/bin/env python
    
    def fib(n):
      if n == 1:
        return 0
      elif n == 2:
        return 1
      return fib(n - 2) + fib(n - 1)
    
    if __name__ == "__main__":
      for i in range(1, 40 + 1):
        print(i, fib(i))
      print("\nPYTHON")
    
  • Ruby
    #!/usr/bin/env ruby
    
    def fib(n)
      return 0 if n == 1
      return 1 if n == 2
      return fib(n - 2) + fib(n - 1)
    end
    
    (1..40).each do |i|
      puts "#{i.to_s} #{fib(i).to_s}"
    end
    puts "\nRUBY"
    

2014-04-09

enum to int in Java

enum to int

  • ElementOfEnum.ordinal()


int to enum

  • EnumType.values()[someInt]


String to enum

  • EnumType.valueOf(NameOfElement)


enum to String

  • ElementOfEnum.name()

2012-09-28

How to fix log4j.dtd XML problem in eclipse

Problem
The file cannot be validated as the XML definition \path\to\workspace\java.shop\src\test\resources\log4j.dtd (The system cannot find the file specified)" that is specified as describing the syntax of the file cannot be located.

Reason
Eclipse cannot find the log4j.dtd for validation.

Solution
Open Preference (Window > Preferences) and Select XML>XML Catalog
Click [Add...] and insert the following
Location: jar:file:C:\USR\eclipse\plugins\org.apache.log4j_1.2.15.v201012070815.jar!/org/apache/log4j/xml/log4j.dtd
Key Type: Public ID
Key: -//APACHE//DTD LOG4J 1.2//EN


2012-07-28

org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER will not be exported or published.

WARNING in Eclipse : Classpath entry org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER will not be exported or published. Runtime ClassNotFoundExceptions may result.
  1. Open [Properties] of the project
  2. Select [Java Build Path]
  3. Select [Order and Export]
  4. Enable checkbox on [Maven Dependencies]
  5. Click [OK]
  6. Open [Windows>Show View>Other>Problems]
  7. Select the warning what you want to solve
  8. Press [Ctrl-1]
  9. Click [Finish] on Quick Fix window

2012-07-27

How to resolve Loader Constraint Violation while running a Spring MVC Project on Tomcat 7

  • Loader Constraint Violation while running a Spring MVC Project on Tomcat 7

  • Error Message
    java.lang.LinkageError: loader constraint violation: when resolving interface method "javax.servlet.jsp.JspApplicationContext.getExpressionFactory()Ljavax/el/ExpressionFactory;" the class loader (instance of org/apache/jasper/servlet/JasperLoader) of the current class, org/apache/jsp/WEB_002dINF/views/home_jsp, and the class loader (instance of org/apache/catalina/loader/StandardClassLoader) for resolved class, javax/servlet/jsp/JspApplicationContext, have different Class objects for the type avax/el/ExpressionFactory; used in the signature

  • Cause
    Tomcat 7 uses el-api 2.2 and jsp-api 2.2.

  • Solution
    1. Open POM.XML file and Go to Dependencies page
    2. Select jsp-api : 2.1 > Click Properties > Change 2.1 to 2.2
    3. Save and run to check

2012-07-15

How to deploy your project on Tomcat within Eclipse

Do you see the message [There are no resources that can be added or removed from the server.] when you select [Add and Remove...] on Tomcat server within Eclipse. If so, you need to add the Eclipse [Dynamic Web Module] facet.
  1. Go to [Properties] on your project.
  2. Check [Dynamic Web Module] at [Project Facets] page.
  3. Add your project on Tomcat and run.
  • Add [Maven Integration for Eclipse WTP] if you cannot find [Dynamic Web Module]

2012-07-14

How to install Eclipse Marketplace client

  1. Click [Help]->[Install New Software...]
  2. Select [Indigo Update Site] at [Work with:]
    If you cannot find it, click [Add...] and then input
    [Name] : Indigo Update Site
    [Location] : http://download.eclipse.org/releases/indigo
    
  3. Check [General Purpose Tools]->[Marketplace Client] and Install it.
  4. Restart Eclipse and See [Help]->[Eclipse Marketplace...]

2012-07-11

How to install Tomcat and IIS

Summary
  1. Add isapi_redirect.dll on IIS
  2. Add [VirtualDirectory] and set [Enabled] on [HandlerMapping]
    Add [ISAPI Filters] on [Web Site] including the added VirtualDirectory
    Add [ISAPI and CGI Restrictions] on [Server]
    

  3. Configure Tomcat
  4. [isapi_redirect.properties] in [TOMCAT_HOME]/ISAPI
    [workers.properties] and [uriworkermap.properties] in [TOMCAT_HOME]/conf
    

How to install Tomcat and IIS
  1. Install JDK, Tomcat(ex, C:\ApacheTomcat), and place Tomcat connector into [TOMCAT_HOME]\ISAPI

  2. Configure IIS
    1. Add Tomcat into [SVR]-[ISAPI and CGI Restrictions]

    2. Add Tomcat into [SVR]-[Handler Mappings] and check Execute on [Edit Feature Permissions]



    3. [Add Virtual Directory], Its name will be used as [extension_uri] of Tomcat Connector


    4. Add ISAPI Filters


  3. Configure Tomcat
    1. Make sure the next 4 elements at [TOMCAT_HOME]/conf/Server.xml
      <Connector port="8080" protocol="HTTP/1.1“ connectionTimeout="20000” redirectPort="8443" />
      <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
      <Host name="localhost"  appBase="webapps” unpackWARs="true" autoDeploy="true“>
      <Context path=" " docBase="C:/Homepage/Tomcat" reloadable="false" />

  4. Configure Tomcat Connector
    1. Make a file [isapi_redirect.properties] with following contents in the folder [TOMCAT_HOME]/ISAPI
      extension_uri=/jakarta/isapi_redirect.dll
      log_file=[TOMCAT_HOME]\logs\isapi_redirect.log
      log_level=info
      worker_file=[TOMCAT_HOME]\conf\workers.properties
      worker_mount_file=[TOMCAT_HOME]\conf\uriworkermap.properties

    2. Make a file [workers.properties] with following contents in the folder [TOMCAT_HOME]/conf
      worker.list=worker1, worker2
      worker.worker1.host=localhost
      worker.worker1.port=8009
      worker.worker1.type=ajp13
      #worker.worker2.host=otherhost
      #worker.worker2.port=8009
      #worker.worker2.type=ajp13
      

    3. Make a file [uriworkermap.properties] with following contents in the folder [TOMCAT_HOME]/conf
      /*.jsp=worker1
      /fedex/*=worker1

How to install Android SDK in SpringSource Tool Suite on Linux Mint and Ubuntu


1.       Install Android SDK
a.       Download Android SDK
b.      Extract the file into any folder. I usually install applications into /opt when I don’t use a package manager.
c.       Run [/opt/android-sdk-linux/tools/android]

e.      Select APIs and Click [Install Packages…]
2.       Setup Eclipse
a.       Run Eclipse. I use SpringSource Tool Suite.
b.      Click [Help > Install New Software…]
c.       Input (https://dl-ssl.google.com/android/eclipse/) in an edit box labeled [Work with:]
d.      Click [Add…]
e.      Input (ADT plugin) in an edit box labeled [Name] and Click [OK]
f.        Click [Select All] and [Developer Tools] and [NDK Plugins] may be selected.
g.       Click [Next >]

How to install SpringSource Tool Suite on Linux Mint and Ubuntu

  1. Download SpringSource Tool Suite
  2. Extract the file into any folder. I usually install applications into /opt when I don’t use a package manager.
  3. Create a menu item
    sudo vi /usr/share/applications/STS.desktop
    
  4. Content
    [Desktop Entry]
    Name=SpringSource Tool Suite
    Comment=SpringSource Tool Suite
    Exec=/opt/springsource/sts-2.9.2.RELEASE/STS
    Icon=/opt/springsource/sts-2.9.2.RELEASE/icon.xpm
    StartupNotify=true
    Terminal=false
    Type=Application
    Categories=Development;IDE;Java;
    
  5. You can see [SpringSource Tool Suite] in [Menu > Programming]