使用ACE Editor在网络上进行Java语言编程

huangapple go评论111阅读模式
英文:

Use ACE Editor for Java Language in web

问题

我希望制作一个类似 ideone.com 的在线 Java 编辑器。

我希望使用 https://ace.c9.io/#nav=about&api=anchor 来创建我的工具。
我已经使用了 codeEditor.session.setMode("ace/mode/java"); 但是我的工具仍然无法编译 Java 语言。

到目前为止,我的代码是:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="lib/css/editor-styles.css">
    <title>Online Test</title>
</head>

<body>
    <div class="editor">
        <div class="editor__wrapper">
            <div class="editor__body">
                <div id="editorCode" class="editor__code"></div>
            </div>
            <div class="editor__footer">
                <div class="editor__footer--left">
                    <button class="editor__btn editor__run">Run &gt;</button>
                    <button class="editor__btn editor__reset">Reset &gt;</button>
                </div>
                <div class="editor__footer--right">
                    <div class="editor__console">
                        <ul class="editor__console-logs"></ul>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <!-- Required Ace Libraries -->
    <script src="lib/js/ace-editor/src-min/ace.js"></script>
    <script src="lib/js/ace-editor/src-min/mode-javascript.js"></script>
    <script src="lib/js/ace-editor/src-min/ext-language_tools.js"></script>

    <!-- Custom Scripts -->
    <script src="lib/js/editor.js"></script>
    <script src="lib/js/editor-console.js"></script>
</body>

</html>

// 检索元素
const consoleLogList = document.querySelector('.editor__console-logs');
const executeCodeBtn = document.querySelector('.editor__run');
const resetCodeBtn = document.querySelector('.editor__reset');

构建用于Web的代码编辑器 - 配置Ace编辑器
构建用于Web的代码编辑器 - 配置Ace编辑器
JS文件

// 设置Ace
let codeEditor = ace.edit("editorCode");
let defaultCode = 'console.log("Editor")';
let consoleMessages = [];

let editorLib = {
clearConsoleScreen() {
consoleMessages.length = 0;

    // 移除日志列表中的所有元素
    while (consoleLogList.firstChild) {
        consoleLogList.removeChild(consoleLogList.firstChild);
    }
},
printToConsole() {
    consoleMessages.forEach(log => {
        const newLogItem = document.createElement('li');
        const newLogText = document.createElement('pre');

        newLogText.className = log.class;
        newLogText.textContent = `> ${log.message}`;

        newLogItem.appendChild(newLogText);

        consoleLogList.appendChild(newLogItem);
    })
},
init() {
    // 配置Ace

    // 主题
    codeEditor.setTheme("ace/theme/monokai");

    // 设置语言
    codeEditor.session.setMode("ace/mode/java");


    // 设置选项
    codeEditor.setOptions({
        fontFamily: 'Inconsolata',
        fontSize: '12pt',
        enableBasicAutocompletion: true,
        enableLiveAutocompletion: true,
    });

    // 设置默认代码
    codeEditor.setValue(defaultCode);
}

}

// 事件
executeCodeBtn.addEventListener('click', () => {
// 清除控制台消息
editorLib.clearConsoleScreen();

// 从代码编辑器获取输入
const userCode = codeEditor.getValue();

// 运行用户代码
try {
    new Function(userCode)();
} catch (err) {
    console.error(err);
}

// 打印到控制台
editorLib.printToConsole();

});

resetCodeBtn.addEventListener('click', () => {
// 清除Ace编辑器
codeEditor.setValue(defaultCode);

// 清除控制台消息
editorLib.clearConsoleScreen();

})

editorLib.init();


<details>
<summary>英文:</summary>

I wish to make an online java editor like ideone.com

I wish to use https://ace.c9.io/#nav=about&amp;api=anchor to make my tool.
I have used codeEditor.session.setMode(&quot;ace/mode/java&quot;); but stil my tool does not compile java language.


Till now my code is :

    &lt;!DOCTYPE html&gt;
    &lt;html lang=&quot;en&quot;&gt;
    
    &lt;head&gt;
        &lt;meta charset=&quot;UTF-8&quot;&gt;
        &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
        &lt;link rel=&quot;stylesheet&quot; href=&quot;lib/css/editor-styles.css&quot;&gt;
        &lt;title&gt;Online Test&lt;/title&gt;
    &lt;/head&gt;
    
    &lt;body&gt;
        &lt;div class=&quot;editor&quot;&gt;
            &lt;div class=&quot;editor__wrapper&quot;&gt;
                &lt;div class=&quot;editor__body&quot;&gt;
                    &lt;div id=&quot;editorCode&quot; class=&quot;editor__code&quot;&gt;&lt;/div&gt;
                &lt;/div&gt;
                &lt;div class=&quot;editor__footer&quot;&gt;
                    &lt;div class=&quot;editor__footer--left&quot;&gt;
                        &lt;button class=&quot;editor__btn editor__run&quot;&gt;Run &gt;&lt;/button&gt;
                        &lt;button class=&quot;editor__btn editor__reset&quot;&gt;Reset &gt;&lt;/button&gt;
                    &lt;/div&gt;
                    &lt;div class=&quot;editor__footer--right&quot;&gt;
                        &lt;div class=&quot;editor__console&quot;&gt;
                            &lt;ul class=&quot;editor__console-logs&quot;&gt;&lt;/ul&gt;
                        &lt;/div&gt;
                    &lt;/div&gt;
                &lt;/div&gt;
            &lt;/div&gt;
        &lt;/div&gt;
        &lt;!-- Required Ace Libraries --&gt;
        &lt;script src=&quot;lib/js/ace-editor/src-min/ace.js&quot;&gt;&lt;/script&gt;
        &lt;script src=&quot;lib/js/ace-editor/src-min/mode-javascript.js&quot;&gt;&lt;/script&gt;
        &lt;script src=&quot;lib/js/ace-editor/src-min/ext-language_tools.js&quot;&gt;&lt;/script&gt;
    
        &lt;!-- Custom Scripts --&gt;
        &lt;script src=&quot;lib/js/editor.js&quot;&gt;&lt;/script&gt;
        &lt;script src=&quot;lib/js/editor-console.js&quot;&gt;&lt;/script&gt;
    &lt;/body&gt;
    
    &lt;/html&gt;

    // Retrieve Elements
    const consoleLogList = document.querySelector(&#39;.editor__console-logs&#39;);
    const executeCodeBtn = document.querySelector(&#39;.editor__run&#39;);
    const resetCodeBtn = document.querySelector(&#39;.editor__reset&#39;);
    

Building a Code Editor for the Web - Configuring Ace Editor
Building a Code Editor for the Web - Configuring Ace Editor
**JS FILE**

    // Setup Ace
    let codeEditor = ace.edit(&quot;editorCode&quot;);
    let defaultCode = &#39;console.log(&quot;Editor&quot;)&#39;;
    let consoleMessages = [];
    
    let editorLib = {
        clearConsoleScreen() {
            consoleMessages.length = 0;
    
            // Remove all elements in the log list
            while (consoleLogList.firstChild) {
                consoleLogList.removeChild(consoleLogList.firstChild);
            }
        },
        printToConsole() {
            consoleMessages.forEach(log =&gt; {
                const newLogItem = document.createElement(&#39;li&#39;);
                const newLogText = document.createElement(&#39;pre&#39;);
    
                newLogText.className = log.class;
                newLogText.textContent = `&gt; ${log.message}`;
    
                newLogItem.appendChild(newLogText);
    
                consoleLogList.appendChild(newLogItem);
            })
        },
        init() {
            // Configure Ace
    
            // Theme
            codeEditor.setTheme(&quot;ace/theme/monokai&quot;);
    
            // Set language
            codeEditor.session.setMode(&quot;ace/mode/java&quot;);
    
    
            // Set Options
            codeEditor.setOptions({
                fontFamily: &#39;Inconsolata&#39;,
                fontSize: &#39;12pt&#39;,
                enableBasicAutocompletion: true,
                enableLiveAutocompletion: true,
            });
    
            // Set Default Code
            codeEditor.setValue(defaultCode);
        }
    }
    
    // Events
    executeCodeBtn.addEventListener(&#39;click&#39;, () =&gt; {
        // Clear console messages
        editorLib.clearConsoleScreen();
        
        // Get input from the code editor
        const userCode = codeEditor.getValue();
    
        // Run the user code
        try {
            new Function(userCode)();
        } catch (err) {
            console.error(err);
        }
    
        // Print to the console
        editorLib.printToConsole();
    });
    
    resetCodeBtn.addEventListener(&#39;click&#39;, () =&gt; {
        // Clear ace editor
        codeEditor.setValue(defaultCode);
    
        // Clear console messages
        editorLib.clearConsoleScreen();
    })
    
    editorLib.init();

</details>


# 答案1
**得分**: 1

> 如何为它添加编译功能?

假设...您可以让您的编辑器将源代码发送到安装有Java编译器的服务器上并运行它。

如果您的应用程序的服务器端是用Java实现的,您*可能*可以利用Java的运行时编译API。请参考https://stackoverflow.com/questions/2946338/how-do-i-programmatically-compile-and-instantiate-a-java-class。 (除非您也想这样做,否则跳过有关加载和运行已编译代码的部分。)

<details>
<summary>英文:</summary>

&gt; How to add compile feature to it?

Hypothetically ... you get your editor to send the source code to a server that has a Java compiler installed and you run it.  

If your app&#39;s server side is implemented in Java, you *could* make use of Java&#39;s runtime compilation APIs.  See https://stackoverflow.com/questions/2946338/how-do-i-programmatically-compile-and-instantiate-a-java-class.  (Skip over the part about loading and running the compiled code ... unless you want to do that too.)

</details>



# 答案2
**得分**: 0

**Ace是一个编辑器,不是编译器。
你不能用它来编译代码。**

Ideone使用Sphere Engine编译器来编译...

请参阅此文档:[链接][1]


  [1]: https://docs.sphere-engine.com/compilers/overview

<details>
<summary>英文:</summary>

**Ace is an editor, not a compiler.
You can&#39;t use it to compile code.**

Ideone use Sphere Engine Compilers to compile... 

Refer this documentation : 
[1] [1]: https://docs.sphere-engine.com/compilers/overview </details>

huangapple
  • 本文由 发表于 2020年10月19日 13:51:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/64421810.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定