- 3,709,613 Users
- 2,241,338 Discussions
- 7,841,260 Comments
Forum Stats
Discussions
Categories
- 9 Data
- 362.2K Big Data Appliance
- 3 Data Science
- 1.1K Databases
- 346 General Database Discussions
- 3.7K Java and JavaScript in the Database
- 22 Multilingual Engine
- 480 MySQL Community Space
- 3 NoSQL Database
- 7.6K Oracle Database Express Edition (XE)
- 2.7K ORDS, SODA & JSON in the Database
- 411 SQLcl
- 33 SQL Developer Data Modeler
- 184.7K SQL & PL/SQL
- 20.9K SQL Developer
- 1.4K Development
- 1 Developer Projects
- 31 Programming Languages
- 134.6K Development Tools
- 4 DevOps
- 3K QA/Testing
- 176 Java
- 3 Java Learning Subscription
- 7 Database Connectivity
- 64 Java Community Process
- Java 25
- 7 Java APIs
- 141.1K Java Development Tools
- 2 Java EE (Java Enterprise Edition)
- 153K Java Essentials
- 132 Java 8 Questions
- 86.1K Java Programming
- 270 Java Lambda MOOC
- 65.1K New To Java
- 1.7K Training / Learning / Certification
- 13.8K Java HotSpot Virtual Machine
- 6 Java SE
- 13.8K Java Security
- 3 Java User Groups
- 22 JavaScript - Nashorn
- 18 Programs
- 83 LiveLabs
- 23 Workshops
- 7 Software
- 3 Berkeley DB Family
- 3.5K JHeadstart
- 5.7K Other Languages
- 2.3K Chinese
- 3 Deutsche Oracle Community
- 6 Español
- 1.9K Japanese
- 2 Portuguese
Hi There, Is it possible to convert a Java Code into Javascript code?

Hi There,
i have a Java code for Generating Encryption Key and I would like it to be in Javascript code, please help.
package encrypt;
import org.apache.commons.codec.binary.Base64;
import javax.crypto.Cipher;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.security.Key;
import java.security.MessageDigest;
import javax.crypto.spec.SecretKeySpec;
public class AESEncryptor
{
private static SecretKeySpec secretKey;
private static byte[] key;
private static String key1;
public static void setKey(final String myKey, final int iterations) {
AESEncryptor.key1 = myKey.toString();
MessageDigest sha = null;
try {
AESEncryptor.key = myKey.getBytes("UTF-8");
sha = MessageDigest.getInstance("SHA-256");
for (int i = 1; i <= iterations; ++i) {
AESEncryptor.key = sha.digest(AESEncryptor.key);
}
AESEncryptor.key = Arrays.copyOf(AESEncryptor.key, 16);
AESEncryptor.secretKey = new SecretKeySpec(AESEncryptor.key, "AES");
}
catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
catch (UnsupportedEncodingException e2) {
e2.printStackTrace();
}
}
}
Answers
Hi,
Please go through the given J2CL document with reference sample code and hope it helps.
Already written java source code with annotation of @JsType can be referred in javascript file and invoked.
Designed, developed, and open-sourced by Google, J2CL is a source-to-source compiler that converts Java to Javascript.
J2CL attempts to solve a different problem than similar Java-to-Javascript frameworks such as GWT, and it is not meant to compete with or replace existing JavaScript frameworks. Instead, J2CL is about interoperability and cross-platform code reuse. According to the developers, "[y]ou can use J2CL to just make some Java code accessible from JavaScript or go all the way to create a whole application with it; whatever best suits your needs."
J2CL can translate most existing Java code from source but not all Java APIs are supported, such as the Java reflection API. By default, translated code is not public and a number of Java annotations from JsInterop can be used to identify what classes, methods, instance variables, or other code should be exposed. The project includes a very simple "hello world" example to demonstrate usage. A slightly modified excerpt of that example is included below showing an annotated Java class and how it can be used in JavaScript.
The following Java class, ran through J2CL:
package com.acme
import jsinterop.annotations.JsType;
@JsType
public class HelloWorld {
public static String getHelloWorld() {
return "Hello from Java!";
}
}
can be used in JavaScript like below:
const HelloWorld = goog.require('com.acme');
console.log(HelloWorld.getHelloWorld());
Regards,
Ravikiran