java.lang.NoSuchMethodError: main
Exception in thread "main"
---------------------------------代码如下import java.io.*;
public class Util extends Object
{
public static int RunThis(String args)
{
Runtime rt = Runtime.getRuntime();
int rc = -1;
try
{
Process p = rt.exec(args);
int bufSize = 4096;
BufferedInputStream bis =
new BufferedInputStream(p.getInputStream(), bufSize);
int len;
byte buffer[] = new byte[bufSize];
while ((len = bis.read(buffer, 0, bufSize)) != -1)
System.out.write(buffer, 0, len);
rc = p.waitFor();
}
catch (Exception e)
{
e.printStackTrace();
rc = -1;
}
finally
{
return rc ;
}
}
}
Exception in thread "main"
---------------------------------代码如下import java.io.*;
public class Util extends Object
{
public static int RunThis(String args)
{
Runtime rt = Runtime.getRuntime();
int rc = -1;
try
{
Process p = rt.exec(args);
int bufSize = 4096;
BufferedInputStream bis =
new BufferedInputStream(p.getInputStream(), bufSize);
int len;
byte buffer[] = new byte[bufSize];
while ((len = bis.read(buffer, 0, bufSize)) != -1)
System.out.write(buffer, 0, len);
rc = p.waitFor();
}
catch (Exception e)
{
e.printStackTrace();
rc = -1;
}
finally
{
return rc ;
}
}
}
解决方案 »
- 求高手解决下动态代理java.lang.ClassCastException: $Proxy0 cannot be cast to
- 关于java中参数使用(String... name1)中的参数有什么特别的用处吗?
- 初学Struts2 if/ifelse标签 使用EL表达式和OGNL表达式的问题
- 救命,还是多态的问题
- 100分求助:Jbuild+oracle用什么连接池呢?
- 请问复制(CTRL+C)是怎么实现的
- 求助!为什么我的JButton按钮没有出来?
- 和Java3d有关,求教!!!!!!!!!!
- 请我在哪里可以下载JSDK包,谢谢!
- 为什么我的UI程序总是把输入法打开,而且操作后刷新总有问题!
- 我要学习用Java的Swing进行比较大项目的UI设计,请问什么工具好些?
- 【JAVA】如何判断文件是否可以访问?
Exception in thread "main"
没有main函数
直接运行
main函数是程序的入口
Exception in thread "main"原因大家都知道,运行一个Java类文件,需要该类文件提供public static void main(String [] args)的入口函数其实程序本身没有问题,但是这段代码只是一个工具类,还需要另外一个类来调用RunThis这个静态函数,为了简单起见,我把入口函数直接写到Util类文件里,获得的代码如下import java.io.*;public class Util extends Object { public static void main(String [] args) {
if (args.length >= 1) {
int r = Util.RunThis(args[0]);
System.out.println("Command executed, return code = " + r);
} else {
System.out.println("Usage: java Util <command1> <command2> ... <commandN>");
}
}
public static int RunThis(String args) {
Runtime rt = Runtime.getRuntime();
int rc = -1;
try {
Process p = rt.exec(args);
int bufSize = 4096;
BufferedInputStream bis = new BufferedInputStream(p
.getInputStream(), bufSize);
int len;
byte buffer[] = new byte[bufSize]; while ((len = bis.read(buffer, 0, bufSize)) != -1)
System.out.write(buffer, 0, len);
rc = p.waitFor();
} catch (Exception e) {
e.printStackTrace();
rc = -1;
}
return rc;
}
}然后随便写一个批处理文件比如1.bat,在命令行执行:java Util 1.bat
运行成功
本来是没有问题的啊但是LZ是在编译的时候没有问题运行时就有这个异常的这个类本来就不是来运行的而是一个javavbean 被其他的类 方法来调用的LZ不需要去运行这个程序编译就可以了
可以按照9楼的去做把
--上面的语句是在Oracle里面的Java source,但是不能达到预期的结果,create or replace and compile java source named Util as
import java.io.*;
import java.lang.*;
public class Util extends Object
{
public static int RunThis(String args)
{
Runtime rt = Runtime.getRuntime();
int rc = -1;
try
{
Process p = rt.exec(args);
int bufSize = 4096;
BufferedInputStream bis =
new BufferedInputStream(p.getInputStream(), bufSize);
int len;
byte buffer[] = new byte[bufSize];
while ((len = bis.read(buffer, 0, bufSize)) != -1)
System.out.write(buffer, 0, len);
rc = p.waitFor();
}
catch (Exception e)
{
e.printStackTrace();
rc = -1;
}
finally
{
return rc;
}
}
}
/create or replace function RUN_CMD(p_cmd in varchar2) return number
as
language java name 'Util.RunThis(java.lang.String) return integer';
/create or replace procedure RC(p_cmd in varchar2)
as
x number;
begin
x := run_cmd(p_cmd);
end;
/
-- 然后进行调用SQL> vari x number;
SQL> set serveroutput on
SQL> exec dbms_java.set_output(100000);PL/SQL procedure successfully completedSQL> exec :x := RUN_CMD('ipconfig');
java.security.AccessControlException: the Permission (java.io.FilePermission <<ALL FILES>> execute) has not been granted to SYSTEM. The PL/SQL to grant this is dbms_java.grant_permission( 'SYSTEM', 'SYS:java.io.FilePermission', '<<ALL FILES>>', 'execute'
)
at java.security.AccessControlContext.checkPermission(AccessControlContext.java)
at java.security.AccessController.checkPermission(AccessController.java)
at java.lang.SecurityManager.checkPermission(SecurityManager.java)
at oracle.aurora.rdbms.SecurityManagerImpl.checkPermission(SecurityManagerImpl.java)
at java.lang.SecurityManager.checkExec(SecurityManager.java)
at java.lang.Runtime.exec(Runtime.java)
at java.lang.Runtime.exec(Runtime.java)
at java.lang.Runtime.exec(Runtime.java)
at java.lang.Runtime.exec(Runtime.java)
at Util.RunThis(UTIL.java:11)PL/SQL procedure successfully completed
x
---------
-1
dbms_java.grant_permission('SYSTEM', 'SYS:java.io.FilePermission', '<<ALL FILES>>', 'execute')用osql执行一遍上面的命令,应该就有权限了感觉楼主从来不看错误提示