1

I have an existing Java project that compiles and runs properly through Eclipse. I have created the following .bat file to run the program sans Eclipse:

java -classpath jflashplayer.jar;bin TestProgram

The file is saved within the project folder, but not within the bin folder (located in same directory as bin). When I try to run the batch, I am met with a large number of runtime errors, the first being

Exception in thread "AWT-EventQueue-0" java.lang.NoClassDefFoundError: org/apache/commons/io/FileUtils

I'm not sure why I get this error when it compiles and runs properly via Eclipse. I have the commons-io jar files linked to the project within Eclipse as libraries, and the jar files are themselves located in the project file (same directory as the batch file and the bin folder).

Also, I'm not entirely sure what the -classpath jflashplayer.jar bit of the batch file is doing. I am using the jflashlayer.jar library (also linked to the project within Eclipse and in the same location as the other jar files), but I am not sure why it would appear in the batch file. I edited an existing batch file from a similar project that uses the jflashplayer.jar files, and it has worked previously to leave that part in.

When I write code in Java, I rarely require it to compile/run outside of the IDE, so I usually have troubles when it comes to this part. Perhaps there is a more robust and foolproof method to run the program outside of the IDE other than the batch file method.

Birrel
  • 4,754
  • 6
  • 38
  • 74

1 Answers1

1

The batch file method is fine, but you have to specify all the libraries you're using on the classpath, just like the jflashplayer.jar.

In this case, the error you're getting is because the Apache commons-io library is not specified on the classpath. Your command would have to look something like:

java -classpath jflashplayer.jar;commons-io.jar;<other jars ...>;bin TestProgram

Alternatively, you can create a runnable jar from Eclipse as described here. When you select a library handling strategy, choose the option Extract required libraries into generated JAR. This will make it so that all the library classes you're using are packaged into your application's jar file, and you can just execute it by invoking java -jar my_application.jar.

Robby Cornelissen
  • 91,784
  • 22
  • 134
  • 156
  • Very helpful, thank you! I added the libraries in the batch file and it all worked out just great! Trying to do the executable jar method and am receiving errors (`JAR export finished with warnings` ... which are `Exported with compile warnings:...` and there are a whole bunch of them, basically every class within the project). – Birrel Aug 18 '14 at 01:50
  • Do these warnings also show up when you're editing your code in Eclipse? Little yellow warning signs to the left of your code? If yes, it's basically those. – Robby Cornelissen Aug 18 '14 at 01:56
  • Oh, yep. A few warnings, but no biggies. The runnable JAR files aren't working though, regardless of how I include the libraries (Extract, Package, Copy). Also, I have to type in the export destination myself and have been creating just random file names (ie `C:\Users\Owner\Desktop\tester.jar`). If it's the file name that is causing the issues, I can fix that, otherwise I'm not sure why it isn't launching the program when I click on the JAR files. – Birrel Aug 18 '14 at 02:00
  • The file name shouldn't matter. Have you tried running the jar from the command line using `java -jar whatever.jar`? To be able to run the JAR by double-clicking it, I found the following [question](http://stackoverflow.com/questions/8511063/how-to-run-jar-file-by-double-click-on-windows-7-64). – Robby Cornelissen Aug 18 '14 at 02:03