文件管理 · 2022年9月8日

java调用脚本文件路径|java如何执行Linux脚本文件

① java中如何得到文件路径

java文件中获得路径Thread.currentThread().getContextClassLoader().getResource("") //获得资源文件(.class文件)所在路径ClassLoader.getSystemResource("")Class_Name.class.getClassLoader().getResource("")Class_Name.class .getResource("/") Class_Name.class .getResource("") // 获得当前类所在路径System.getProperty("user.dir") // 获得项目根目录的绝对路径System.getProperty("java.class.path") //得到类路径和包路径打印输出依次如下:file:/F:/work_litao/uri_test/WebContent/WEB-INF/classes/file:/F:/work_litao/uri_test/WebContent/WEB-INF/classes/file:/F:/work_litao/uri_test/WebContent/WEB-INF/classes/file:/F:/work_litao/uri_test/WebContent/WEB-INF/classes/file:/F:/work_litao/uri_test/WebContent/WEB-INF/classes/com/xml/imp/F:\work_litao\uri_testF:\work_litao\uri_test\WebContent\WEB-INF\classes;F:\work_litao\uri_test\WebContent\WEB-INF\lib\dom4j.jar

② java程序读取资源文件时路径如何指定

(1)、request.getRealPath("/");//不推荐使用获取工程的根路径 (2)、request.getRealPath(request.getRequestURI());//获取jsp的路径,这个方法比较好用,可以直接在servlet和jsp中使用 (3)、request.getSession().getServletContext().getRealPath("/");//获取工程的根路径,这个方法比较好用,可以直接在servlet和jsp中使用 (4)、 this.getClass().getClassLoader().getResource("").getPath();//获取工程classes 下的路径,这个方法可以在任意jsp,servlet,java文件中使用,因为不管是jsp,servlet其实都是java程序,都是一个 class。所以它应该是一个通用的方法。0、关于绝对路径和相对路径1.基本概念的理解绝对路径:绝对路径就是你的主页上的文件或目录在硬盘上真正的路径,(URL和物理路径)例 如:C:xyz est.txt 代表了test.txt文件的绝对路径。http://www.sun.com/index.htm也代表了一个URL绝对路径。相对路径:相对与某个基 准目录的路径。包含Web的相对路径(HTML中的相对目录),例如:在Servlet中,"/"代表Web应用的跟目录。和物理路径的相对表示。例 如:"./" 代表当前目录,"../"代表上级目录。这种类似的表示,也是属于相对路径。另外关于URI,URL,URN等内容,请参考RFC相关文档标准。RFC 2396: Uniform Resource Identifiers (URI): Generic Syntax,(http://www.ietf.org/rfc/rfc2396.txt)2.关于JSP/Servlet中的相对路径和绝对路径。 2.1服务器端的地址服务器端的相对地址指的是相对于你的web应用的地址,这个地址是在服务器端解析的(不同于html和javascript中的相对 地址,他们是由客户端浏览器解析的)1、request.getRealPath方法:request.getRealPath("/") 得到的路径:C:\Program Files\Apache Software Foundation\Tomcat 5.5\webapps\strutsTest\方法:request.getRealPath(".") 得到的路径:C:\Program Files\Apache Software Foundation\Tomcat 5.5\webapps\strutsTest\.方法:request.getRealPath("") 得到的路径:C:\Program Files\Apache Software Foundation\Tomcat 5.5\webapps\strutsTestrequest.getRealPath("web.xml") C:\Program Files\Apache Software Foundation\Tomcat 5.5\webapps\strutsTest\web.xml2、request.getParameter("");ActionForm.getMyFile();方法:String filepath = request.getParameter("myFile"); 得到的路径:D:\VSS安装目录\users.txt方法:String filepath = ActionForm.getMyFile(); 得到的路径:D:\VSS安装目录\users.txt————————————————– strutsTest 为工程名myFile 在ActionForm中,为private String myFile; 在jsp页面中:为<html:file property="myFile"></html:file>

③ java获取某个文件夹的路径怎么写

File类有两个常用方法可以得到文件路径一个是:getCanonicalPath(),另一个是:getAbsolutePath(),可以通过File类的实例调用这两个方法例如file.getAbsolutePath()其中file是File的实例对象。下面是一个具体例子:public class PathTest{ public static void main(String[] args) { File file = new File(".\\src\\"); System.out.println(file.getAbsolutePath()); try { System.out.println(file.getCanonicalPath()); } catch (IOException e) { e.printStackTrace(); } }}getAbsolutePath()和getCanonicalPath()的不同之处在于,getCanonicalPath()得到的是一个规范的路径,而getAbsolutePath()是用构造File对象的路径+当前工作目录。例如在上面的例子中.(点号)代表当前目录。getCanonicalPath()就会把它解析为当前目录但是getAbsolutePath()会把它解析成为目录名字(目录名字是点号)。下面是上面程序在我电脑上的输出:G:\xhuoj\konw\.\src\G:\xhuoj\konw\src\

④ java调用txt文件的路径问题

/* 读入TXT文件 */ String pathname = "D:\\twitter\\13_9_6\\dataset\\en\\input.txt"; // 绝对路径或相对路径都可以,这里是绝对路径。File filename = new File(pathname); // 要读取以上路径的input。txt文件 /* 写入Txt文件 */ // 相对路径,如果没有则要建立一个新的output.txt文件 File writename = new File(".\\result\\en\\output.txt");

⑤ 如何在java中执行shell脚本

import java.io.IOException; public class test { public static void main(String[] args) throws IOException,InterruptedException { //Runtime.getRuntime().exec("/Users/lijialiang/codetest/stop.sh jmeter"); //Runtime.getRuntime().exec(new String[]{"/bin/sh","-c","/codetest/stop.sh jmeter"}); //Runtime.getRuntime().exec(new String[]{"/bin/sh","-c","ps -ef | grep jmeter | grep -v 'grep' |awk '{print $2}'>>result.txt"}); Runtime.getRuntime().exec(new String[]{"/bin/sh","-c","ps -ef | grep <span style="font-family: Arial, Helvetica, sans-serif;">jmeter</span><span style="font-family: Arial, Helvetica, sans-serif;"> | awk '{print $2}' | xargs kill -9"});</span> //Thread.sleep(100000); } }

⑥ java如何执行linux脚本文件

1)在linux下写一个.sh文件(2)文件内容如下:复制代码 代码如下:#!/bin/sh //bash文件头APP_HOME=/home/blmcrm/crm/A //要执行的java文件中bin文件的上一个目录,我的目录是/home/blmcrm/crm/A/bin/blm……(后面不写了),总之就是写bin目录前面的部分,因为jar包在bin目录里面,如果不在bin里面,也如此改一下就行CLASSPATH=$APP_HOME/bin //bin目录当然是包含jar包的目录啦for i in "$APP_HOME"/bin/*.jar //引进所有的jar包,这里用的循环,当然也可以按照这个格式一个一个 写do CLASSPATH="$CLASSPATH":"$i" //环境变量就这格式doneexport CLASSPATH=.:$CLASSPATH //不写这个可能会说找不到main类echo ${CLASSPATH} //打印环境变量,可以不写java -Xms50m -Xmx250m org.gdh.blm.exec.Syslog_csv exit(结束) //执行java程序, 其中-Xms50m -Xmx250m是运行内存什么的设置,后面的是bin目录以后一直到.class的路径,我的是Syslog_csv.class,所以我最后那个是Syslog_csv。

⑦ 怎么通过java去调用并执行shell脚本以及问题总结

// 用法:Runtime.getRuntime().exec("命令"); String shpath="/test/test.sh"; //程序路径 Process process =null; String command1 = “chmod 777 ” + shpath; try { Runtime.getRuntime().exec(command1 ).waitFor(); } catch (IOException e1) { e1.printStackTrace(); }catch (InterruptedException e) { e.printStackTrace(); }

⑧ 如何在java程序中调用linux命令或者shell脚本

楼上说的是对的,我贴一个我写的一个方法给你做参考。/** * 根据Linux命令获取磁盘的剩余空间,根据传入的参数size比较,如果大于size就返回ture,否则返回false * @param command * @param size * @return */ public static boolean isAvailableOnLinux(String command ,Long size) { //这里使用 df +文件夹路径 查看磁盘的信息 Long space=0L; InputStreamReader ir = null; LineNumberReader input = null; Process process = null; try { //获取linux进程 process = Runtime.getRuntime().exec(command); //新建流 ir = new InputStreamReader(process.getInputStream()); input = new LineNumberReader(ir); String line; int i = 1; while ((line = input.readLine()) != null) { if(i==2){ //使用正则表达式分隔字符串 String[]s=line.split("\\s+"); //第2行,第4列的值为该磁盘的可用空间大小 space=Long.parseLong(s[3]); } i++; } } catch (java.io.IOException e) { System.err.println("IOException " + e.getMessage()); } finally{ //关闭流 if (input != null) { try { input.close(); } catch (IOException e) { e.printStackTrace(); } } if (ir != null) { try { ir.close(); } catch (IOException e) { e.printStackTrace(); } } } if(space>size){ return true; }else{ return false; } }

⑨ windows下java怎样调用shell脚本文件

// 用法:Runtime.getRuntime().exec("命令"); String shpath="/test/test.sh"; //程序路径 Process process =null; String command1 = “chmod 777 ” + shpath; try { Runtime.getRuntime().exec(command1 ).waitFor(); } catch (IOException e1) { e1.printStackTrace(); }catch (InterruptedException e) { e.printStackTrace(); }String var="201102"; /参数 String command2 = “/bin/sh ” + shpath + ” ” + var; Runtime.getRuntime().exec(command2).waitFor();

⑩ 在java项目中如何获取某个文件的路径

如果是在tomcat等服务器中运行的话,用ServletContext中的getRealPath()方法可以获取指定文件的绝对路径,如:getRealPath("/WEB-INF/db.xml");