258

When I run tests I get the error "Command line is too long". It works if I set the "Shorten command line" method in the Run/Debug configuration to "JAR manifest" for the specific method or class, but how do I set it for the whole project or is there an IDE global setting for it?

naXa stands with Ukraine
  • 35,493
  • 19
  • 190
  • 259
momo
  • 2,723
  • 2
  • 11
  • 10

10 Answers10

293

Inside your .idea folder, change workspace.xml file

Add

<property name="dynamic.classpath" value="true" />

to

  <component name="PropertiesComponent">
.
.
.
  </component>

Example

 <component name="PropertiesComponent">
    <property name="project.structure.last.edited" value="Project" />
    <property name="project.structure.proportion" value="0.0" />
    <property name="project.structure.side.proportion" value="0.0" />
    <property name="settings.editor.selected.configurable" value="preferences.pluginManager" />
    <property name="dynamic.classpath" value="true" />
  </component>

If you don't see one, feel free to add it yourself

 <component name="PropertiesComponent">
    <property name="dynamic.classpath" value="true" />
  </component>

Community edit: this solution does not seem to work in newer versions of IDE, the format of PropertiesComponent is different now. If that's your case, then this answer should work

Klesun
  • 12,280
  • 5
  • 59
  • 52
Maulzey
  • 4,100
  • 5
  • 22
  • 30
  • 5
    This method works in the event that your version of intellij doesn't have the GUI method of setting this. I'd like to point out, it may be the .idea/workspace.xml file OR .iws file. If you don't have a .idea folder, search for an .iws file in your project. – Dennis Bartlett Feb 25 '19 at 17:14
  • 1
    Thanks! It works for me in Android Studio 4.0 – DungPhan Dec 15 '20 at 02:47
  • 1
    This didn't work for me, although obviously working for others so not sure why – Daniel Wilson Jul 21 '21 at 16:18
  • you can do this in .iml file as well if you cant find .iws file or /.idea directory – Alok Mishra Dec 16 '21 at 12:56
172

Intellij 2018.2.5

Run => Edit Configurations => Choose Node on the left hand side => expand Environment => Shorten Command line options => choose Classpath file or JAR manifest

Screen shot of Run/Debug Configuration showing the command line options

Rajesh Goel
  • 3,277
  • 1
  • 17
  • 13
  • 21
    This solution has to be set new every time you run a single test you haven't run before – 4ndro1d Jan 07 '19 at 12:34
  • 5
    You can set this on the template for the configuration type you are setting up and it will not need to be set for every new test/configuration. See the accepted answer for details. – Dennis Bartlett Feb 25 '19 at 17:11
  • 1
    Yup! This worked like a charm for me for Android tests! Thank you! – sud007 Jun 11 '20 at 08:42
127

You can set up a default way to shorten the command line and use it as a template for further configurations by changing the default JUnit Run/Debug Configuration template. Then all new Run/Debug configuration you create in project will use the same option.

Here is the related blog post about configurable command line shortener option.

Andrey
  • 15,144
  • 25
  • 91
  • 187
  • 8
    It doesnt work anymore in the new version because the option got deleted. How to do it now? – Mulgard Apr 25 '18 at 13:16
  • 56
    The setting that worked for me is to select option "classpath file" on the dialog that comes up from clicking the link the error message. This is under the setting "Shorten command line". – havoc1 Oct 04 '18 at 14:06
  • 3
    select option "classpath file" works and I would suggest to include this option in the answer for newer version of IDE. – MrKumar Nov 14 '19 at 17:42
  • 2
    @MrKumar i get: 'CommandLineWrapper' is ill-suited for launching apps on Java 9+. If the run configuration uses "classpath file", please change it to "@argfile". Otherwise, please contact support. – Tym Pollack Aug 20 '20 at 21:22
  • 3
    Hey @TymPollack I got that too and fixed it by going to Run -> Edit Configurations. In the Shorten Command Line section, I chose user-local option and it worked. Try it see if it works. I am on AS 4.2 – Beast77 May 07 '21 at 12:58
  • 3
    In IJ version 2021, the "Shorten command line" option is hidden by default. To see it, choose "Modify options" -> check "Shorten command line". – kabeen Jun 08 '21 at 08:22
69

The latest 2020 build doesn't have the shorten command line option by default we need to add that option from the configuration.

Run > Edit Configurations > Select the corresponding run configuration and click on Modify options for adding the shorten command-line configuration to the UI. enter image description here

Select the shorten command line option enter image description here

Now choose jar manifest from the shorten command line option enter image description here

Jinu P C
  • 3,146
  • 1
  • 20
  • 29
53

Thanks to Rajesh Goel in Android Studio:

Run > Edit Configurations...

enter image description here

Select a test (better to select a parent test class) and set a Shorten command line: option to classpath file. Then OK (or Apply, OK).

enter image description here

CoolMind
  • 26,736
  • 15
  • 188
  • 224
18

If you use JDK version from 9+, you should select

Run > Edit Configurations... > Select JUnit template.

Then, select @argfile (Java 9+) as in the image below.

enter image description here

If you don't see the Shorten command line, then just clicking on Modify options menu as below, and select the Shorten command line

enter image description here

Please try it. Good luck friends.

turong
  • 1,474
  • 9
  • 14
9

Add <property name="dynamic.classpath" value="true" /> to the .idea/workspace.xml file under the <component name="PropertiesComponent"> tag. That worked for me.

4

Follow this steps and select "classpath.file" option from the dropdown list given in shorten command line category. enter image description here

enter image description here

2

In my case, the error was:

Error running 'MasterFileGenerator':
Command line is too long. Shorten command line for StagingFileGenerator or also for Application default configuration.

The solution was to switch to go to "Run/Debug Configurations" and switch from user-local default: none - java [options] className [args] to JAR manifest - java -cp classpath.jar className [args].

enter image description here

Jaime Montoya
  • 6,915
  • 14
  • 67
  • 103
0

To have it persisted:

  1. Open Intellj
  2. ctrl+shift+n
  3. Type workspace.xml
  4. Find component name="PropertiesComponent" tag
  5. a) in case there are list of properties, add

<property name="dynamic.classpath" value="true" />

b) in case there is keyToString json, add new line in the map:

&quot;dynamic.classpath&quot;: &quot;true&quot; (remember about comma in the upper line)

  1. (optional) If you want to have this config enabled also in another project immediately, restart intellj
Krzysiek
  • 21
  • 3