在javascript中eval()可以实现字符串转代码,java中需要使用动态编译.
把获得的字符串写入一个临时文件中,然后编译它,在调用其中的函数.
我们把要转换的字符串构造一个完整的类:如果方法是有返回值的.则:
public object eval(string str){
//生成java文件
string s = "class temp{";
s += "object rt(){"
s += "myclass mc = new myclass();"
s += " return mc."+str+"();";
s += "}"
s +="}";
file f = new file("temp.java");
printwriter pw = new printwriter(new filewriter(f));
pw.println(s);
pw.close();
//动态编译
com.sun.tools.javac.main javac = new com.sun.tools.javac.main();
string[] cpargs = new string[] {"-d", "所在目录","temp.java"};
int status = javac.compile(cpargs);
if(status!=0){
system.out.println("没有成功编译源文件!");
return null;
}
//调用temp的rt方法返回结果:
myclassloader mc = new myclassloader();
class clasz = mc.loadclass("test.class",true);
method rt = clasz.getmethod("rt", new class[]{ string[].class });
return rt.invoke(null, new object[] { new string[0] });
//如果方法没有返回就直接调用
我们可以先写好多个重载的eval,有返回值和没有返回值的.以及可以传递参数的.
这样我们就可以用字符串转换为java的语句来执行.
将字符串类型的时间转换成date类型可以使用SimpleDateFormat来转换,具体方法如下: 第一段:定义一个字符串类型的时间; 第二段:创建一个SimpleDateFormat对象并设置格式; 第三段:最后使用SimpleDateFormat的parse方法将String类型的时间转换成Date类型的时间. 具体代码如下: String string = "二0一四-三-一漆"; SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); Date date = null; try { date = dateFormat.parse(string); System.out.println(date.toLocaleString().split(" ")[0]);//切割掉不要的时分秒数据 } catch (ParseException e) { e.printStackTrace(); } 介绍一下SimpleDateFormat:SimpleDateFormat 是 Java 中一个非常常用的类,该类用来对日期字符串进行解析和格式化输出
Java实现字符串转换成可执行代码
使用commons的jexl可实现将字符串变成可执行代码的功能,我写了一个类来封装这个功能:
import java.util.Map;
/**
* 动态加载方法
*
*/
public class DyMethodUtil {
public static Object invokeMethod(String jexlExp,MapString,Object map){
JexlEngine jexl=new JexlEngine();
Expression e = jexl.createExpression(jexlExp);
JexlContext jc = new MapContext();
for(String key:map.keySet()){
jc.set(key, map.get(key));
if(null==e.evaluate(jc)){
return "";
return e.evaluate(jc);
调用
MapString,Object map=new HashMapString,Object();
map.put("testService",testService);
map.put("person",person);
String expression="testService.save(person)";
DyMethodUtil.invokeMethod(expression,map);
代码如下:
import?java.util.Arrays;
import?java.util.Scanner;
public?class?App?{
public?static?void?main(String[]?args)?{
Scanner?scanner?=?new?Scanner(System.in);
//?1.键盘录入一个字符串
String?str?=?scanner.nextLine();
char[]?chars?=?new?char[str.length()];
for?(int?i?=?0;?i?str.length();?i++)?{
char?ch?=?str.charAt(i);
chars[i]?=?ch;
if?(chars[0]?!=?chars[chars.length?-?1])?{
char?ch?=?chars[0];
chars[0]?=?chars[chars.length?-?1];
chars[chars.length?-?1]?=?ch;
System.out.println(Arrays.toString(chars));
public?static?void?main(String?args[])
{
String?a[]=?str.split("\\+");//+是特殊字符,需要用\\转义,split返回string数组
System.out.println("截取结果:"+a[0]+"+"+a[1]);
int?sum=Integer.parseInt(a[0])+Integer.parseInt(a[1]);
System.out.println("运算结果:"+sum);
java字符串如何解析成运行的java代码
public static void main(String[] args) {
int i = 10;
code += "for(int i=0;i" + i + ";i++){";
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.compile(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();
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;
以上就是土嘎嘎小编为大家整理的java把字符串变成代码相关主题介绍,如果您觉得小编更新的文章只要能对粉丝们有用,就是我们最大的鼓励和动力,不要忘记讲本站分享给您身边的朋友哦!!