方法是有的,但不全實(shí)際.
為企業(yè)提供成都網(wǎng)站制作、成都做網(wǎng)站、網(wǎng)站優(yōu)化、網(wǎng)絡(luò)營銷推廣、競價托管、品牌運(yùn)營等營銷獲客服務(wù)。成都創(chuàng)新互聯(lián)公司擁有網(wǎng)絡(luò)營銷運(yùn)營團(tuán)隊,以豐富的互聯(lián)網(wǎng)營銷經(jīng)驗助力企業(yè)精準(zhǔn)獲客,真正落地解決中小企業(yè)營銷獲客難題,做到“讓獲客更簡單”。自創(chuàng)立至今,成功用技術(shù)實(shí)力解決了企業(yè)“網(wǎng)站建設(shè)、網(wǎng)絡(luò)品牌塑造、網(wǎng)絡(luò)營銷”三大難題,同時降低了營銷成本,提高了有效客戶轉(zhuǎn)化率,獲得了眾多企業(yè)客戶的高度認(rèn)可!
首先,你要理解,JAVA代碼的運(yùn)行是要被編譯成字節(jié)碼codebyte后才能被執(zhí)行的,以你問題中的意思,那個字符串應(yīng)該是在怎么代碼被編譯后項目在運(yùn)行時,再傳入的參數(shù),這時,要想把傳入?yún)?shù)的字符串當(dāng)成JAVA代碼來運(yùn)行,只有一種辦法,就是把傳進(jìn)來的字符串轉(zhuǎn)換成字節(jié)碼,并且,要虛擬加一個方法體出來.這也不是不可行,可用的技術(shù)有asm.cglib等字節(jié)碼技術(shù).但你想,對于你的問題.這樣有實(shí)際的意義嗎
你還不如,把system.out.print重定義成新的外調(diào)方法調(diào)動來的清晰
java字符串如何解析成運(yùn)行的java代碼
有些情況下,不得不動態(tài)運(yùn)行Java代碼,以便提供更加靈活的方式,以下代碼可參考(在JDK 1.5+平臺上運(yùn)行通過):
public static void main(String[] args) {
int i = 10;
String code = "System.out.println(\"Hello World!\"+(13+2*5/3));";
code += "for(int i=0;i" + i + ";i++){";
code += " System.out.println(Math.pow(i,2));";
code += "}";
try {
run(code);
} catch (Exception e) {
e.printStackTrace();
}
}
private synchronized static File compile(String code) throws Exception {
File file = File.createTempFile("JavaRuntime", ".java", new File(System.getProperty("user.dir")));
file.deleteOnExit();
// 獲得類名
String classname = getBaseFileName(file);
// 將代碼輸出到文件
PrintWriter out = new PrintWriter(new FileOutputStream(file));
out.println(getClassCode(code, classname));
out.close();
// 編譯生成的java文件
String[] cpargs = new String[] { "-d",
System.getProperty("user.dir") + "\\WebRoot\\WEB-INF\\classes",
file.getName() };
int status = Main點(diǎn)抗 pile(cpargs);
if (status != 0) {
throw new Exception("語法錯誤!");
}
return file;
}
private static synchronized void run(String code) throws Exception {
String classname = getBaseFileName(compile(code));
new File(System.getProperty("user.dir")
+ "\\WebRoot\\WEB-INF\\classes\\" + classname + ".class")
.deleteOnExit();
try {
Class cls = Class.forName(classname);
Method main = cls.getMethod("method", null);
main.invoke(cls, null);
} catch (Exception se) {
se.printStackTrace();
}
}
private static String getClassCode(String code, String className) {
StringBuffer text = new StringBuffer();
text.append("public class " + className + "{\n");
text.append(" public static void method(){\n");
text.append(" " + code + "\n");
text.append(" }\n");
text.append("}");
return text.toString();
}
private static String getBaseFileName(File file) {
String fileName = file.getName();
int index = fileName.indexOf(".");
String result = "";
if (index != -1) {
result = fileName.substring(0, index);
} else {
result = fileName;
}
return result;
}
如,String
aa
=
new
String(
"aa
")
這條語句存儲在數(shù)據(jù)庫的表中。
如果你的code變量的內(nèi)容格式都是這樣固定的
可以先解析字符串
得到類、方法和參數(shù)的信息
然后可使用反射動態(tài)調(diào)用該方法
~如果你認(rèn)可我的回答,請及時點(diǎn)擊【采納為滿意回答】按鈕
~~手機(jī)提問的朋友在客戶端右上角評價點(diǎn)【滿意】即可。
~你的采納是我前進(jìn)的動力
~~O(∩_∩)O,記得好評和采納,互相幫助,謝謝。
網(wǎng)頁標(biāo)題:字符串解析為java代碼 字符串的解析
路徑分享:http://chinadenli.net/article0/ddegooo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供外貿(mào)建站、網(wǎng)站制作、商城網(wǎng)站、品牌網(wǎng)站設(shè)計、定制開發(fā)、網(wǎng)站營銷
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)