0

So I want to run my program from console. I copied the same execution line that Eclipse used to run my program:

"C:\Program Files\Java\jre7\bin\javaw.exe" -Dfile.encoding=Cp1257 -classpath C:\Users\MyName\workspace\MyProject\bin;C:\Users\MyName\workspace\MyProject\lib\commons-io-2.4.jar files.Start

Figured that it will not run it so I changed it to this:

java C:\Users\MyName\workspace\MyProject\lib\commons-io-2.4.jar files.Start

I run it at the root folder of my project and java will throw this error:

Error: Could not find or load main class files.Start

What is the problem in here? It runs fine with Eclipse.

My error

Veske
  • 547
  • 1
  • 4
  • 15
  • @Reimeus, Error is obviously different. – Veske May 12 '14 at 17:15
  • Nope, the error message is slightly different because it's being hit at a slightly different point in the loading sequence, but the cause is identical. – chrylis -cautiouslyoptimistic- May 12 '14 at 17:27
  • Why did you "figure that it will not run"? Did you try it? The only change you should have made was changing `javaw` to `java`, and even that wouldn't strictly be necessary. – David Conrad May 12 '14 at 17:48
  • It will give an error that some token is unexpected in the `-Dfile` part – Veske May 12 '14 at 17:51
  • That's surprising. What is the error? Also, I think you had `-classpath` in the one you used, otherwise it should have complained about not being able to find or load main class the-whole-path-to-that-jar. – David Conrad May 12 '14 at 17:55
  • I added a illustrative picture of the command used and the error to my original post. – Veske May 12 '14 at 18:00
  • It is complaining about the -Dfile for some reason indeed. And if I remove it, it will complain about the `-classpath` – Veske May 12 '14 at 18:06
  • What are you using for a shell? I've tried it with Cygwin bash and with Windows cmd.exe and it works. I'm guess that it's Windows PowerShell, and maybe you need to quote that entire parameter? I'm not familiar with PowerShell, though, so I can't say. – David Conrad May 12 '14 at 18:09
  • I got it fixed by not using the PowerShell. – Veske May 12 '14 at 18:10

2 Answers2

2

After some experimenting with Windows PowerShell, it seems that it's necessary to quote both the definition of the file.encoding property and the classpath.

java "-Dfile.encoding=Cp1257" -classpath "C:\Users\MyName\workspace\MyProject\bin;C:\Users\MyName\workspace\MyProject\lib\commons-io-2.4.jar" files.Start

If you're launching it from your project directory, this should be enough:

java "-Dfile.encoding=Cp1257" -classpath "bin;lib\commons-io-2.4.jar" files.Start
David Conrad
  • 15,432
  • 2
  • 42
  • 54
1

You don't have the -classpath in the second command-line.

Tripp Kinetics
  • 5,178
  • 2
  • 23
  • 37
  • Allright I did a: ´java -jar C:\Users\MyName\workspace\MyProject\lib\commons-io-2.4.jar -classpath files.Start´ and it tells me that I have no main manifest attribute in my .jar file`? – Veske May 12 '14 at 17:30
  • There shouldn't be a `-jar` parameter; the jar is just on the classpath, it's being used by `files.Start` but it isn't an executable jar file. The `-classpath` parameter has to precede the semicolon-delimited list of paths/jars on the classpath. – David Conrad May 12 '14 at 17:45
  • Something like this? `java -cp bin:lib/commons-io-2.4.jar files.Start` This is runned from root directory of project. Sadly it still produces the same error. – Veske May 12 '14 at 17:48
  • That's because it has to be a semicolon, not a colon. Also, watch out if the shell you're using treats semicolons specially (as bash does); you may need to quote or escape it. – David Conrad May 12 '14 at 17:58
  • Yes!! It seemns that the PowerShell does not like a semicolon indeed. I just used the old console and everything is working as it should. Thank you! – Veske May 12 '14 at 18:10