執行儲存在字串中的程式碼
問題:
可以執行下列Java代碼嗎?儲存在字串變數中?是否可以將這樣的 String 轉換為 Java 語句並運行它?
答案:
使用編譯器API
正如之前的建議中提到的,編譯器API 使您能夠動態編譯和執行程式碼。以下是如何使用它:
<code class="java">// Create a Java source code string String javaCode = "..."; // Create a Java compiler instance JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); // Create a source code file from the string JavaFileObject javaFile = new StringJavaFileObject("MyCode", javaCode); // Compile the source code DiagnosticCollector<javafileobject> diagnostics = new DiagnosticCollector(); Iterable extends JavaFileObject> compilationUnits = Collections.singletonList(javaFile); compiler.getTask(null, null, diagnostics, null, null, compilationUnits).call(); // Execute the compiled code Class> clazz = Class.forName("MyCode"); Method method = clazz.getDeclaredMethod("myMethod"); method.invoke(null);</javafileobject></code>
使用Beanshell
或者,您可以如下使用Beanshell 庫:
<code class="java">// Create a Beanshell interpreter and add the Java code to it Interpreter interpreter = new Interpreter(); interpreter.eval(javaCode); // Access the defined classes and methods in the interpreter Class> myClass = interpreter.getClassLoader().loadClass("MyClass"); Method method = myClass.getDeclaredMethod("myMethod"); method.invoke(null);</code>
請注意,Beanshell 不再積極開發,但在生產環境中仍然可靠。
以上是如何執行儲存在字串變數中的Java程式碼?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

Java在不同操作系統上的表現存在細微差異。 1)JVM實現不同,如HotSpot、OpenJDK,影響性能和垃圾回收。 2)文件系統結構和路徑分隔符不同,需使用Java標準庫處理。 3)網絡協議實現差異影響網絡性能。 4)GUI組件外觀和行為在不同系統上有別。通過使用標準庫和虛擬機測試,可減少這些差異的影響,確保Java程序穩定運行。

javaoffersrobustobject-IentiendedProgrammming(OOP)和Top-Notchsecurityfeatures.1)OopinjavainCludesClasses,對象,繼承,多態性,和列出,andeclingfleximaintainablesys.ss.2)SecurityFeateTuersLudEtersludEterMachine(

JavaScriptandJavahavedistinctstrengths:JavaScriptexcelsindynamictypingandasynchronousprogramming,whileJavaisrobustwithstrongOOPandtyping.1)JavaScript'sdynamicnatureallowsforrapiddevelopmentandprototyping,withasync/awaitfornon-blockingI/O.2)Java'sOOPf

JAVAACHIEVESPLATFORMINDEPENTENCETHROUGHJAVAVIRTAILMACHINE(JVM)和BYTECODE.1)THEJVMINTERPRETSBBYTECODE,允許theingthesmecodetorunonanyanyanyanyplatformwithajvm.2)

java'splatformendependecemeansapplicationscanrunonanyplatformwithajvm,使“ Writeonce,runanywhere”。

JVM'SperformanceIsCompetitiveWithOtherRuntimes,operingabalanceOfspeed,安全性和生產性。 1)JVMUSESJITCOMPILATIONFORDYNAMICOPTIMIZAIZATIONS.2)c提供NativePernativePerformanceButlanceButlactsjvm'ssafetyFeatures.3)

JavaachievesPlatFormIndependencEthroughTheJavavIrtualMachine(JVM),允許CodeTorunonAnyPlatFormWithAjvm.1)codeisscompiledIntobytecode,notmachine-specificodificcode.2)bytecodeisisteredbytheybytheybytheybythejvm,enablingcross-platerssectectectectectross-eenablingcrossectectectectectection.2)

TheJVMisanabstractcomputingmachinecrucialforrunningJavaprogramsduetoitsplatform-independentarchitecture.Itincludes:1)ClassLoaderforloadingclasses,2)RuntimeDataAreafordatastorage,3)ExecutionEnginewithInterpreter,JITCompiler,andGarbageCollectorforbytec


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

SublimeText3 英文版
推薦:為Win版本,支援程式碼提示!

禪工作室 13.0.1
強大的PHP整合開發環境

SublimeText3漢化版
中文版,非常好用