Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
快速查找Java是从Windows cmd还是Cygwin终端启动的_Java_Windows_Cygwin_Command Prompt_Terminal Emulator - Fatal编程技术网

快速查找Java是从Windows cmd还是Cygwin终端启动的

快速查找Java是从Windows cmd还是Cygwin终端启动的,java,windows,cygwin,command-prompt,terminal-emulator,Java,Windows,Cygwin,Command Prompt,Terminal Emulator,我有一个Java应用程序,可以从Windows命令提示符和Cygwin终端使用。该程序使用和操作文件路径。当程序从Cygwin启动时,sep变量将是/,但当程序从Windows启动时,\变量将是非常有用的 听着,我不确定这是否可能,但我想问问 我将发布一个小的、可编译的应用程序,在几分钟内显示这个问题。现在,我只想说我想要一组函数,类似于: // in main ... String sep = getSeparatorToUse(); ... // member functions ...

我有一个Java应用程序,可以从Windows命令提示符和Cygwin终端使用。该程序使用和操作文件路径。当程序从Cygwin启动时,
sep
变量将是
/
,但当程序从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()
适用于:在文件名/文件路径中使用相对文件路径和空格

$ 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格式的完整文件路径:

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

我只是从我的原始代码中删减,所以如果它看起来太大,我很抱歉

/**********************************
 * @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

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

java.io.File.separator;
无论如何。。。要找出哪个SO java正在运行(不确定它是如何检测到cygwin的),请尝试:


这是我想出的一个答案,几乎回答了我最初的问题。它尝试根据filename参数确定Java代码的启动器。非常感谢@Raphael_Moita和@Luke_Lee,他们几乎解决了我的问题。他们的解决方案没有回答最初的问题,但部分原因是我没有完全发布最初的问题。正如我所说,这个答案并没有完全回答最初的问题。如果有人知道完整的解决方案,请让我知道

我的解决办法是几种方法。就目前而言,它们只适用于我的案例——Windows上的Cygwin。(他们所做的是告诉您Java应用程序的filename参数是否与从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, e.g., "/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, e.g. 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,但我也需要参考。最后一个问题有助于解决我的特殊情况,即文件并非全部托管在
C:\
驱动器上

注意
我不会接受我的答案,因为它不能回答我原来的问题。我希望它能帮助某人回答最初的问题。

在有人提问之前,我确实知道
cygpath
。如果我知道文件路径将使用UNIX风格的路径输入,我可以使用:
java$(cygpath-wp)
,这在我的情况下不起作用。无论如何,这是没有意义的,因为那些将从Cygwin使用该程序的人在需要将其UNIX样式的路径转换为Windows样式的路径时不会遇到问题。您可以始终使用
/
。这些都是很好的建议。当我自己在Linux上运行代码时,
isWindows
将特别有用。一个问题是
java.io.File.separator即使从Cygwin终端运行,也会返回“\”。它并没有完全回答我的问题,这就是为什么我没有选择它作为首选答案,但再一次,我没有完全问我的问题。非常感谢!我今天一直在考虑这个问题,你完全正确;我不需要知道哪个操作系统在我的Java下。我可以从filename参数中去掉
/cygwin/c
(如果存在
/cygwin/c
部分)。唯一的问题是当filename参数来自不同的驱动器时,可能是一个公共的、安装的驱动器
/cygdrive/p
。我在最初的帖子中没有提到这一点,但是我想知道Java是从哪个终端/提示符启动的。
boolean isWindows = System.getProperty("os.name").startsWith("win");
// 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, e.g., "/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, e.g. 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)