快速查找Java是否从Windows cmd或Cygwinterminal启动

我有一个将从Windows命令提示符和Cygwinterminal使用的Java应用程序。 该程序使用和操作文件path。 当程序从Cygwin启动时,我们会非常有用一个sepvariables,而当程序从Windows启动的时候是\\

看这里 ,我不确定这是可能的,但我想问。

我将发布一个小的,可编译的应用程序,在几分钟内显示该问题。 现在,我只是说我想要一组函数,就像这样:

 // in main ... String sep = getSeparatorToUse(); ... // member functions ... private boolean wasLaunchedFromWinCmd() { if (<something-here-that-knows-it-was-cmd-not-cygwin>) return true; return false; }//endof: private boolean wasLaunchedFromWinCmd() private String getSeparatorToUse() { if (wasLaunchedFromWinCmd) return "\\" return "/" }//endof: private String getSeparatorToUse() 

谢谢@Raphael_Moita。 这些是非常有用的,我可能会在我将要使用的Linux版本的应用程序中使用它们。 @Luke_Lee,我觉得哑巴没有意识到它。 我想你们两个可能已经解决了我的问题,而我准备好了可编译的代码。 当程序从批处理脚本运行时,仍然有一个问题 – 当它从find命令提供一个文件名。 我希望我所展示的将会说明这一点。

例子

所有的例子都是从Cygwin运行的。

工作:大多数志愿者使用代码的方式,只是与java代码位于同一目录中的文件名。

 $ java FileSeparatorExample pic_4.jpg Here, something will be done with the file, C:\Users\bballdave025\Desktop\pic_4.jpg 

工作:在文件名/文件path中使用相对的文件path和空格

 $ java FileSeparatorExample pretty\ pictures/pic\ 1.jpg Here, something will be done with the file, C:\Users\me\Desktop\pretty pictures/pic 1.jpg $ java FileSeparatorExample ../pic_5.jpg Here, something will be done with the file, C:\Users\me\Desktop\../pic_5.jpg 

不工作。 有时, find命令的输出将以Cygwin / UNIX格式的完整文件path出现:

 $ java FileSeparatorExample /cygdrive/c/David/example/pic.jpg The file: C:\Users\bballdave025\Desktop\/cygdrive/c/David/example/pic.jpg doesn't exist 

可编译的代码

我只是从原来的代码中减less,所以如果看起来太大,我感到抱歉。

 /********************************** * @file FileSeparatorExample.java **********************************/ // Import statements import java.io.File; import java.io.IOException; public class FileSeparatorExample { // Member variables private static String sep; public static void main(String[] args) { ////****** DOESN'T WORK AS DESIRED ******//// sep = java.io.File.separator; ////** I want **//// // sep = getFileSeparator(); String imageToLoad = null; boolean argumentExists = ( args != null && args.length != 0 ); if (argumentExists) { boolean thereIsExactlyOneArgument = ( args.length == 1 ); if (thereIsExactlyOneArgument) { imageToLoad = args[0]; }//endof: if (thereIsExactlyOneArgument) else { // do some other stuff } }//endof: if (argumentExists) String filenamePath = getFilenamePath(imageToLoad); String filenameFile = getFilenameFile(imageToLoad); imageToLoad = filenamePath + sep + filenameFile; File f = new File(imageToLoad); if (! f.exists()) { System.err.println("The file:"); System.err.println(imageToLoad); System.err.println("doesn\'t exist"); System.exit(1); }//endof: if (! f.exists()) System.out.println("Here, something will be done with the file,"); System.out.println(imageToLoad); }//endof: main // member methods /** * Separates the filename arg into: full path to directory; bare filename */ private static String[] splitFilename(String imageToLoad) { String[] fileParts = new String[2]; int indexOfLastSep = imageToLoad.lastIndexOf(sep); boolean fullFilenameHasSeparator = ( indexOfLastSep != -1 ); if (fullFilenameHasSeparator) { fileParts[0] = imageToLoad.substring(0, indexOfLastSep); fileParts[1] = imageToLoad.substring(indexOfLastSep + 1); }//endof: if (fullFilenameHasSeparator) else { // Use the user's directory as the path fileParts[0] = System.getProperty("user.dir"); fileParts[1] = imageToLoad; }//endof: if/else (fullFilenameHasSeparator) return fileParts; }//endof: private static String[] splitFilename(String imageToLoad) /** * Gives the full path to the file's directory (from the filename arg) * but not the bare filename */ private static String getFilenamePath(String imageToLoad) { String[] fileParts = splitFilename(imageToLoad); return fileParts[0]; }//endof: private static String getFilenamePath(String imageToLoad) /** * Gives the bare filename (no path information) */ private static String getFilenameFile(String imageToLoad) { String[] fileParts = splitFilename(imageToLoad); return fileParts[1]; }//endof: private static String getFilenamePath(String imageToLoad) }//endof: public class FileSeparatorExample 

你不需要知道你的Java下是哪个。 如果您的目标是找到正确的文件分隔符来使用,请调用:

 java.io.File.separator; 

无论如何…要找出是哪个Java正在运行(不知道如何检测到cygwin),请尝试:

 boolean isWindows = System.getProperty("os.name").startsWith("win"); 

这是我提出的答案, 几乎回答了我原来的问题。 它试图根据文件名参数来确定Java代码的启动器。 非常感谢@Raphael_Moita和@Luke_Lee,他们几乎解决了我的问题。 他们的解决方案没有回答原来的问题,但这部分是因为我没有完全提出原来的问题。 正如我所说, 这个答案完全不回答原来的问题。 如果有人知道完整的解决方案,请让我知道。

我的解决方案是一些方法。 他们站在一边,他们只为我的情况工作 – Cygwin在Windows上。 (他们所做的是告诉你Java应用程序的文件名参数是否与从Windows cmd启动一致。)我打算发布一个更便携的方法组,即其他操作系统。

我确定有问题。 请把它们指给我。

 // in main ... sep = java.io.File.separator; // Thanks @Luke_Lee if (args != null && args.length != 0) sep = getSeparatorToUse(args[0]); ... // member functions ... private boolean wasLaunchedFromWinCmd(String firstArg) { boolean isWindows = System.getProperty("os.name").startsWith("win"); if (! isWindows) return false; // Thanks @Raphael_Moita else { String launchDir = System.getProperty("user.dir"); String rootOfLaunchDir = getRoot(launchDir); // This will come back with something like "C:\" or "P:\" String rootOfArgument = getRoot(firstArg); if (rootOfArgument.equals("/")) { String cygwinBase = "/cygdrive/"; char letterOfRoot = rootOfLaunchDir.charAt(0); // For, eg, "/Users/me/Desktop/pic_314.jpg" if (firstArg.startsWith(cygwinBase)) { int charsToCut = cygwinBase.length(); letterOfRoot = firstArg.substring(charsToCut, charsToCut + 1); }//endof: if (firstArg.startsWith(cygwinBase)) System.out.println("The root directory of your argument will be:"); System.out.println(Character.toUpperCase(letterOfRoot) + ":\\"); System.out.println("In Cygwin, that will be:"); System.out.println(cygwinBase + Character.toLowerCase(letterOfRoot) + "/"); return false; // Not always correct, eg if someone in Cygwin uses // $ java FileSeparatorExample "C:\pic_137.jpg" }//endof: if (rootOfArgument.equals("/")) return true; }//endof: if/else (! isWindows) }//endof: private boolean wasLaunchedFromCmd() private String getRoot(String fileOrDir) { File file = new File(fileOrDir).getAbsoluteFile(); File root = file.getParentFile(); while (root.getParentFile() != null) root = root.getParentFile(); return root.toString(); }//endof: private String getRoot(); private String getSeparatorToUse(String firstArg) { if (wasLaunchedFromWinCmd(firstArg)) return "\\" return "/" }//endof: private String getSeparatorToUse(String firstArg) 

这个解决方案的一部分是由于@Raphael_Moita和@Luke_Lee,但我也需要引用这个SO帖子 。 这最后一个帮助我的具体情况,文件不是所有托管在C:\驱动器。

注意我不会接受我的正确解决方案,因为它不回答我原来的问题。 我希望这可以帮助回答原来的问题。