How to set a long Java classpath in Windows?

后端 未结 12 950
清酒与你
清酒与你 2020-11-27 15:18

I\'m trying to run a particular JUnit test by hand on a Windows XP command line, which has an unusually high number of elements in the class path. I\'ve tried several varia

相关标签:
12条回答
  • 2020-11-27 15:39

    You could try this


    @echo off
    set A=D:\jdk1.6.0_23\bin
    set B=C:\Documents and Settings\674205\Desktop\JavaProj
    set PATH="%PATH%;%A%;"
    set CLASSPATH="%CLASSPATH%;%B%;"
    

    go to a command prompt and run it twice(no idea why....i have to do so on a windows XP machine) also the paths r set only for the current command prompt session

    0 讨论(0)
  • 2020-11-27 15:40

    As HuibertGill mentions, I would wrap this in an Ant build script just so that you don't have to manage all of this yourself.

    0 讨论(0)
  • 2020-11-27 15:45

    Thanks to Raman for introducing a new solution to a pathing problem for Java 9+. I made a hack to bootRun task that allows using everything already evaluated by gradle to run java with argument files. Not very elegant but working.

    // Fix long path problem on Windows by utilizing java Command-Line Argument Files 
    // https://docs.oracle.com/javase/9/tools/java.htm#JSWOR-GUID-4856361B-8BFD-4964-AE84-121F5F6CF111 
    // The task creates the command-line argument file with classpath
    // Then we specify the args parameter with path to command-line argument file and main class
    // Then we clear classpath and main parameters
    // As arguments are applied after applying classpath and main class last step 
    // is done to cheat gradle plugin: we will skip classpath and main and manually
    // apply them through args
    // Hopefully at some point gradle will do this automatically 
    // https://github.com/gradle/gradle/issues/1989 
    
    if (Os.isFamily(Os.FAMILY_WINDOWS)) {
        bootRun {
            doFirst {
                def argumentFilePath = "build/javaArguments.txt"
                def argumentFile = project.file(argumentFilePath)
                def writer = argumentFile.newPrintWriter()
                writer.print('-cp ')
                writer.println(classpath.join(';'))
                writer.close()
    
                args = ["@${argumentFile.absolutePath}", main]
                classpath = project.files()
                main = ''
            }
        }
    }
    
    
    0 讨论(0)
  • 2020-11-27 15:46

    Use An "Argument File" on Java 9+

    In Java 9+, the java executable supports providing arguments via a file. See https://docs.oracle.com/javase/9/tools/java.htm#JSWOR-GUID-4856361B-8BFD-4964-AE84-121F5F6CF111.

    This mechanism is explicitly intended to solve the problem of OS limitations on command lengths:

    You can shorten or simplify the java command by using @argument files to specify a text file that contains arguments, such as options and class names, passed to the java command. This let’s you to create java commands of any length on any operating system.

    In the command line, use the at sign (@) prefix to identify an argument file that contains java options and class names. When the java command encounters a file beginning with the at sign (@) , it expands the contents of that file into an argument list just as they would be specified on the command line.

    This is the "right" solution, if you are running version 9 or above. This mechanism simply modifies how the argument is provided to the JVM, and is therefore 100% compatible with any framework or application, regardless of how they do classloading i.e. it is completely equivalent to simply providing the argument on the command line as usual. This is not true for manifest-based workarounds to this OS limitation.

    An example of this is:

    Original command:

    java -cp c:\foo\bar.jar;c:\foo\baz.jar

    can be rewritten as:

    java @c:\path\to\cparg

    where c:\path\to\cparg is a file which contains:

    -cp c:\foo\bar.jar;c:\foo\baz.jar
    

    This "argument file" also supports line continuation characters and quoting for properly handling spaces in paths e.g.

    -cp "\
    c:\foo\bar.jar;\
    c:\foo\baz.jar"
    

    Gradle

    If you are encountering this issue in Gradle, see this plugin, which converts your classpath automatically into an "argument file" and provides that to the JVM when doing exec or test tasks on Windows. On Linux or other operating systems it does nothing by default, though an optional configuration value can be used to apply the transformation regardless of OS.

    https://github.com/redocksoft/classpath-to-file-gradle-plugin

    (disclaimer: I am the author)

    See also this related Gradle issue -- hopefully this capability will eventually be integrated into Gradle core: https://github.com/gradle/gradle/issues/1989.

    0 讨论(0)
  • 2020-11-27 15:53

    I think you are up the creek without a paddle here. The commandline has a limit for arguments to call a programm.

    I have 2 sugestion you could try. First, prior to running the junit tests, you can let a script/ant_task create JARs of the various classes on the classpath. Then you can put the JARs on the classpath, which should be shorter.

    Another way you could try is to create an antscript to run JUNIT, in ANT there should not be such a limit for classpath entries.

    0 讨论(0)
  • 2020-11-27 15:55

    Fix for windows gradle long classpath issue. Fixes JavaExec tasks that error out with message "CreateProcess error=206, The filename or extension is too long"

    Using the plugins DSL:

    plugins {
      id "com.github.ManifestClasspath" version "0.1.0-RELEASE"
    }
    

    Using legacy plugin application:

    buildscript {
      repositories {
        maven {
          url "https://plugins.gradle.org/m2/"
        }
      }
      dependencies {
        classpath "gradle.plugin.com.github.viswaramamoorthy:gradle-util-plugins:0.1.0-RELEASE"
      }
    }
    
    apply plugin: "com.github.ManifestClasspath"
    
    0 讨论(0)
提交回复
热议问题