本文整理汇总了Java中org.apache.commons.exec.OS类的典型用法代码示例。如果您正苦于以下问题:Java OS类的具体用法?Java OS怎么用?Java OS使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
OS类属于org.apache.commons.exec包,在下文中一共展示了OS类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: executeWSCommand
import org.apache.commons.exec.OS; //导入依赖的package包/类
public void executeWSCommand() throws Throwable {
if (OS.isFamilyWindows()) {
throw new SkipException("Test not valid for Windows");
}
JavaProfile profile = new JavaProfile(LaunchMode.JAVA_WEBSTART).addWSArgument("-verbose").addVMArgument("-Dx.y.z=hello");
final CommandLine commandLine = profile.getCommandLine();
AssertJUnit.assertNotNull(commandLine);
AssertJUnit.assertTrue(commandLine.toString().contains("-javaagent:"));
AssertJUnit.assertTrue(commandLine.toString().contains("-verbose"));
AssertJUnit.assertTrue(commandLine.toString().contains("-Dx.y.z=hello"));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
commandLine.copyOutputTo(baos);
commandLine.executeAsync();
new Wait("Waiting till the command is complete") {
@Override public boolean until() {
return !commandLine.isRunning();
}
};
BufferedReader reader = new BufferedReader(new StringReader(new String(baos.toByteArray())));
String line = reader.readLine();
while (line != null && !line.contains("Web Start")) {
line = reader.readLine();
}
AssertJUnit.assertTrue(line.contains("Web Start"));
}
示例2: isLinux
import org.apache.commons.exec.OS; //导入依赖的package包/类
/**
* test is Linux
*
* @return true if linux
*/
public static boolean isLinux() {
if (_linux == -1) {
try {
if (OS.isFamilyUnix()) {
String uname = Shell.run("uname -a");
log.debug("uname -a=" + uname);
_linux = uname.indexOf("Linux") > -1 ? 1 : 0;
} else {
_linux = 0;
}
} catch (Exception e) {
log.error(e.getMessage(), e);
}
}
return _linux == 1;
}
示例3: getExecScriptFileOrNull
import org.apache.commons.exec.OS; //导入依赖的package包/类
private File getExecScriptFileOrNull(String scriptNameBase) {
String resource = null;
if (OS.isFamilyWindows()) {
resource = scriptNameBase + ".bat";
} else if (OS.isFamilyUnix()) {
resource = scriptNameBase + ".sh";
}
File resourceFile = getClasspathResourceFileOrNull(resource);
// TODO use canExecute here (available since java 1.6)
if (resourceFile != null && !resourceFile.canRead()) {
logger.warn("The resource " + resourceFile.getAbsolutePath() + " is not readable!");
// it is not readable, do not try to execute it
return null;
}
return resourceFile;
}
示例4: findExecutable
import org.apache.commons.exec.OS; //导入依赖的package包/类
static String findExecutable( final String executable, final List<String> paths )
{
File f = null;
search: for ( final String path : paths )
{
f = new File( path, executable );
if ( !OS.isFamilyWindows() && f.isFile() )
break;
else
for ( final String extension : getExecutableExtensions() )
{
f = new File( path, executable + extension );
if ( f.isFile() )
break search;
}
}
if ( f == null || !f.exists() )
return null;
return f.getAbsolutePath();
}
示例5: searchForServerDirectory
import org.apache.commons.exec.OS; //导入依赖的package包/类
/**
* Search the operating system for an Appium server installation directory.
*
* @return A File representation to the Appium server installation
* directory.
*/
private File searchForServerDirectory() {
if (OS.isFamilyWindows()) {
if (getArch().equals("32")) {
return doesDirectoryExists(System.getenv("ProgramFiles")
+ "/Appium");
} else {
// must be the x86_64
return doesDirectoryExists(System.getenv("ProgramFiles")
+ " (x86)/Appium");
}
} else if (OS.isFamilyMac()) {
return doesDirectoryExists(APPIUM_SERVER_MAC_DEFAULT_DIRECTORY);
}
// server directrory was not found.
throw new ServerDirectoryNotFoundException();
}
示例6: init
import org.apache.commons.exec.OS; //导入依赖的package包/类
private void init() throws MojoExecutionException {
// Supply variables that are OS dependent
if (OS.isFamilyWindows()) {
if (command == null) {
command = "taskkill";
}
} else if (OS.isFamilyUnix() || OS.isFamilyMac()) {
if (command == null) {
command = "kill";
}
} else {
if (command == null) {
throw new MojoExecutionException(
"Unknown OS - You must use the 'command' parameter");
}
}
}
示例7: applyFileMode
import org.apache.commons.exec.OS; //导入依赖的package包/类
private static void applyFileMode(final File file, final FileMode fileMode)
throws MojoExecutionException {
if (OS.isFamilyUnix() || OS.isFamilyMac()) {
final String smode = fileMode.toChmodStringFull();
final CommandLine cmdLine = new CommandLine("chmod");
cmdLine.addArgument(smode);
cmdLine.addArgument(file.getAbsolutePath());
final Executor executor = new DefaultExecutor();
try {
final int result = executor.execute(cmdLine);
if (result != 0) {
throw new MojoExecutionException("Error # " + result + " while trying to set mode \""
+ smode + "\" for file: " + file.getAbsolutePath());
}
} catch (final IOException ex) {
throw new MojoExecutionException("Error while trying to set mode \"" + smode + "\" for file: "
+ file.getAbsolutePath(), ex);
}
} else {
file.setReadable(fileMode.isUr() || fileMode.isGr() || fileMode.isOr());
file.setWritable(fileMode.isUw() || fileMode.isGw() || fileMode.isOw());
file.setExecutable(fileMode.isUx() || fileMode.isGx() || fileMode.isOx());
}
}
示例8: testExecute
import org.apache.commons.exec.OS; //导入依赖的package包/类
@Test
public void testExecute() throws MojoExecutionException, IOException {
// PREPARE
final EventStorePostStartMojo testee = new EventStorePostStartMojo();
final File dir = new File("./src/test").getCanonicalFile();
testee.setEventStoreDir(dir);
if (OS.isFamilyWindows()) {
testee.setPostStartCommand(dir + File.separator + "echotest.bat");
} else {
testee.setPostStartCommand(dir + File.separator + "echotest.sh");
}
// TEST
testee.execute();
// VERIFY
assertThat(testee.getMessages()).contains("Hello world!");
}
示例9: checkGivenExecutableIsUsed
import org.apache.commons.exec.OS; //导入依赖的package包/类
public void checkGivenExecutableIsUsed() throws Throwable {
JavaProfile profile = new JavaProfile(LaunchMode.JAVA_APPLET);
File f = findFile();
profile.setAppletURL(f.getAbsolutePath());
profile.setStartWindowTitle("Applet Viewer: SwingSet3Init.class");
String actual = "";
if (OS.isFamilyWindows()) {
String path = System.getenv("Path");
String[] split = path.split(";");
File file = new File(split[0]);
File[] listFiles = file.listFiles();
if (listFiles != null) {
for (File listFile : listFiles) {
if (listFile.getName().contains(".exe")) {
profile.setJavaCommand(listFile.getAbsolutePath());
actual = listFile.getAbsolutePath();
break;
}
}
}
} else {
actual = "ls";
profile.setJavaCommand(actual);
}
CommandLine commandLine = profile.getCommandLine();
AssertJUnit.assertTrue(commandLine.toString().contains(actual));
}
示例10: getProcessStatus
import org.apache.commons.exec.OS; //导入依赖的package包/类
/**
* get the status of the processname.
*
* @param processname
* the process name
* @return the status of the process
* @throws IOException
* throw IOException if occur error
*/
public static String getProcessStatus(String processname) throws IOException {
if (isLinux() || OS.isFamilyMac()) {
String line = "ps -ef";
String r = run(line);
StringBuilder sb = new StringBuilder();
String[] ss = r.split("\n");
if (ss != null && ss.length > 0) {
for (String s : ss) {
if (s.contains(processname)) {
sb.append(s).append("\n");
}
}
}
return sb.toString();
} else if (OS.isFamilyWindows()) {
String cmd = "tasklist /nh /FI \"IMAGENAME eq " + processname + "\"";
return run(cmd);
} else {
throw new IOException("not support");
}
}
示例11: getStartScriptName
import org.apache.commons.exec.OS; //导入依赖的package包/类
public static String getStartScriptName() throws URISyntaxException {
String base="Transkribus.";
if (OS.isFamilyWindows()) {
String exe = base+"exe";
String cmd = "cmd /c start "+exe;
// cmd += "& del "+getCurrentJar().getName(); // this cleans the old version in windows after the new version has started --> should work, as current JVM should exit sooner than new program has started!
return cmd;
} else if (OS.isFamilyMac()) {
return "./"+base+"command";
} else {
return "./"+base+"sh";
}
}
示例12: getClasspathArg
import org.apache.commons.exec.OS; //导入依赖的package包/类
private String getClasspathArg() {
String classpath = System.getProperty("java.class.path");
if (OS.isFamilyWindows()) {
// On windows the ";" character is replaced by a space by the
// command interpreter. Thus the classpath is split with the
// ;-token. Therefore the classpath should be quoted with double
// quotes
classpath = "\"\"" + classpath + "\"\"";
} else {
// quote only once
classpath = "\"" + classpath + "\"";
}
return classpath;
}
示例13: createEnvWrapperFile
import org.apache.commons.exec.OS; //导入依赖的package包/类
protected File createEnvWrapperFile( File envScript )
throws IOException
{
PrintWriter writer = null;
File tmpFile = null;
try
{
if ( OS.isFamilyWindows() )
{
tmpFile = File.createTempFile( "env", ".bat" );
writer = new PrintWriter( tmpFile );
writer.append( "@echo off" ).println();
writer.append( "call \"" ).append( envScript.getCanonicalPath() ).append( "\"" ).println();
writer.append( "echo " + EnvStreamConsumer.START_PARSING_INDICATOR ).println();
writer.append( "set" ).println();
writer.flush();
}
else
{
tmpFile = File.createTempFile( "env", ".sh" );
// tmpFile.setExecutable( true );//java 6 only
writer = new PrintWriter( tmpFile );
writer.append( "#! /bin/sh" ).println();
writer.append( ". " ).append( envScript.getCanonicalPath() ).println(); // works on all unix??
writer.append( "echo " + EnvStreamConsumer.START_PARSING_INDICATOR ).println();
writer.append( "env" ).println();
writer.flush();
}
}
finally
{
IOUtil.close( writer );
}
return tmpFile;
}
示例14: testGetExecutablePathPreferExecutableExtensionsOnWindows
import org.apache.commons.exec.OS; //导入依赖的package包/类
public void testGetExecutablePathPreferExecutableExtensionsOnWindows()
throws IOException
{
// this test is for Windows
if (!OS.isFamilyWindows()) {
return;
}
final ExecMojo realMojo = new ExecMojo();
final String tmp = System.getProperty("java.io.tmpdir");
final File workdir = new File(tmp, "testGetExecutablePathPreferExecutableExtensionsOnWindows");
workdir.mkdirs();
final Map<String, String> enviro = new HashMap<String, String>();
final File f = new File(workdir, "mycmd");
final File fCmd = new File(workdir, "mycmd.cmd");
f.createNewFile();
fCmd.createNewFile();
assertTrue( "file exists...", f.exists() );
assertTrue( "file exists...", fCmd.exists() );
realMojo.setExecutable( "mycmd" );
final CommandLine cmd = realMojo.getExecutablePath( enviro, workdir );
// cmdline argumets are: [/c, %path-to-temp%\mycmd.cmd], so check second argument
assertTrue( "File should have cmd extension",
cmd.getArguments()[1].endsWith( "mycmd.cmd" ) );
f.delete();
fCmd.delete();
assertFalse( "file deleted...", f.exists() );
assertFalse( "file deleted...", fCmd.exists() );
}
示例15: getCommandLineAsString
import org.apache.commons.exec.OS; //导入依赖的package包/类
private String getCommandLineAsString( CommandLine commandline )
{
// for the sake of the test comparisons, cut out the eventual
// cmd /c *.bat conversion
String result = commandline.getExecutable();
boolean isCmd = false;
if ( OS.isFamilyWindows() && result.equals( "cmd" ) )
{
result = "";
isCmd = true;
}
String[] arguments = commandline.getArguments();
for ( int i = 0; i < arguments.length; i++ )
{
String arg = arguments[i];
if ( isCmd && i == 0 && "/c".equals( arg ) )
{
continue;
}
if ( isCmd && i == 1 && arg.endsWith( ".bat" ) )
{
arg = arg.substring( 0, arg.length() - ".bat".length() );
}
result += ( result.length() == 0 ? "" : " " ) + arg;
}
return result;
}