#Java to Javascript
1 messages ยท Page 1 of 1 (latest)
Here is an AI assisted attempt to answer your question ๐ค. Maybe it helps! In any case, a human is on the way ๐. To continue talking to the AI, you can use </chatgpt:1108714622413963314>.
I'm sorry, but there seems to be some confusion in your statement. Java 19 does not exist as a version of Java. The latest stable version of Java is Java 17 as of September 2021.
If you want to export variables from a Java class and use them in JavaScript, you can achieve this by using a library like GraalVM or by using a server-client architecture where the Java backend exposes an API that the JavaScript frontend can consume.
Here's a general outline of how you can accomplish this:
-
Set up your Java backend: Create a RESTful API using a framework like Spring Boot or Javalin. In your API, define endpoints that expose the variables you want to use in JavaScript.
-
Build and run your Java backend: Compile and run your Java code to start the backend server.
-
Set up your JavaScript frontend: Create an HTML file with embedded JavaScript or use a framework like React or Angular to build your frontend application.
-
Consume the API in JavaScript: Use AJAX requests or fetch API to make HTTP requests to the endpoints exposed by your Java backend. Retrieve the variables from the response and use them in your JavaScript code.
Here's an example using AJAX:
// Make an AJAX request to retrieve variables from the Java backend
var xhr = new XMLHttpRequest();
xhr.open('GET', '/api/variables', true);
xhr.onreadystatechange = function() {
if (xhr.readyState === 4 && xhr.status === 200) {
var response = JSON.parse(xhr.responseText);
var variable1 = response.variable1;
var variable2 = response.variable2;
// Use the variables in your JavaScript code
console.log(variable1);
console.log(variable2);
}
};
xhr.send();
Note that this is just a basic example, and you may need to adapt it based on your specific requirements and frameworks used.
Remember to ensure that your Java backend is running and accessible by the JavaScript frontend for this approach to work.
how are you exposing them? Over a rest call or?
I'm sorry i'm new i don't know what's a rest call
I assume you're using JS in a web page (unless you're using Nodejs for example), and java in a backend application. How are you "exporting" these variables? How do you want to use them?
I'm using nodejs, i'm using the library aes with javascript to encode and decode text, password, and all that stuff, i wanna be able to write the text in the js from a java class that I'm using to write JSON files, so that before it writes the text it's already encoded
So you want to directly call your java function with the encoded things from your Node code? There's https://www.npmjs.com/package/java-caller for example. Not sure if that's a real win over doing that directly from node though
ok thx
you're welcome
why ? why can't you do that in js directly ?