Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Java to JavaScript Without calling it from Main Function #1005

Open
TamilpriyaSync opened this issue Feb 7, 2025 · 1 comment
Open

Java to JavaScript Without calling it from Main Function #1005

TamilpriyaSync opened this issue Feb 7, 2025 · 1 comment

Comments

@TamilpriyaSync
Copy link

TamilpriyaSync commented Feb 7, 2025

I have 3 simple java files in src/main/java/org/teavm/helper directory:

Main.java
package org.teavm.helper;

public class Main {
public static void main(String[] args) {
System.out.println("Hi From Client.java");

    Parent parent = new Parent();
    parent.showMessage(10);

    Child child = new Child();
    child.showMessage(10); 
}

}

Parent.java
package org.teavm.helper;

public class Parent {
String name = "Parent";

void showMessage(int number) {
    int doubled = number*2;
    System.out.println("Parent Class doubles " + number + " to: " + doubled);
}

}

Child.java
package org.teavm.helper;

class Child extends Parent {

@Override
void showMessage(int number) {
    int tripled = number*3;
    System.out.println("Child Class triples " + number + " to: " + tripled);
}

}

Goal: I want to convert the Parent.java and Child.java Java classes to JavaScript using TeaVM without calling them from the Main.java. When I achieve this, I will be able to scale this to a larger project.

Is it possible to achieve this without invoking them directly in Main.java? If yes, how can I set up TeaVM to convert these classes to JavaScript successfully without an entry point in Main.java? Kindly help me to achieve this

Thank you in advance.

PS: I tried the below ways by referring the internet:
Import Annotation - org.teavm.interop.Import is actually used to use JS code into Java code.
Export Annotation - org.teavm.interop doesn't contain 'Export'
Static Block - Build was successful but the JS was not created
Preserve Annotation - org.teavm.interop doesn't contain 'Preserve'
JS Body Annotation - JS requires more manual code rewrite
Class Reflection - Only @ReflectClass annotation throws error

@konsoletyper
Copy link
Owner

Sure. Please, read this: https://teavm.org/docs/runtime/js-modules.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants