1

I am having problem to run the main class from the jar.

Ant script has produced following folders:

MyProject(Somewhere in C:)
 |
 |
 |____configuration(this contains properties/XML file)
 |
 |____dist(contains MyProject.jar)
 |
 |____lib(contains all other jars)
 |
 |____run(contains batch file to run MyProject.jar)

Inside run folder I have a batch file which reads like this:

java -jar ../dist/MyProject.jar;../lib/*.jar com.my.test.MainTest

Can somebody guide me. I just want to go to run folder and double click the .bat file and run the application.

I am getting

Exception in thread "main" java.lang.NoClassDefFoundError: MyProject/jar

Update

The new Error is:

The java class is not found:  com.microsoft.sqlserver.jdbc.SQLServerException

Thanks...

Vineet Reynolds
  • 72,899
  • 16
  • 143
  • 173
user234194
  • 1,623
  • 9
  • 35
  • 54

3 Answers3

6

It seems like you are passing in multiple JAR files to the java application launcher. That's not how it works.

You'll need to pass in a singular jar file (MyProject.jar in this case), which serves as the entry point. All related JARs should be specified in ClassPath entries of the manifest MANIFEST.MF, of the main jar. The manifest should also specify the Main class - the one having the main() method.

If you want to avoid the above, and specify the complete classpath on the command line, use the -cp or -classpath flag. However, you'll need to specify wildcards on the classpath, in a manner different from the one listed in the question. The following might work; on Windows, wrap the classpath entries in quotes if needed:

REM notice the quotes in the cp argument. Those are to be omitted in *nix
java -cp "../dist/MyProject.jar;../lib/*" com.my.test.MainTest

Update

Based on the new error message now reported, it appears that the Microsoft SQL Server JDBC Driver is not present in the classpath. This would require downloading and placing the necessary JARs (in the lib directory). If the driver is present elsewhere, then the above command used to launch the application should be updated with the location of the JAR.

Vineet Reynolds
  • 72,899
  • 16
  • 143
  • 173
  • What is the new error? Can you post the new stack trace in the question? It would help. – Vineet Reynolds Jun 06 '11 at 23:16
  • I already have the jar file under lib folder. It runs from the eclipse....but when running this way I am not able to load the jar file...thanks... – user234194 Jun 07 '11 at 02:36
  • Have you tried printing out the classpath, just before the point of failure? You can do so, using `System.out.println(System.getProperty("java.class.path"));`. That would give you a hint of whether all the required JARs have been considered. – Vineet Reynolds Jun 07 '11 at 02:43
  • When I print it, I am getting ../lib/*;../dist/Myproject.jar – user234194 Jun 07 '11 at 14:03
  • I suppose that means the lib directory has the sqljdbc.jar or sqljdbc4.jar file. Would I be right? – Vineet Reynolds Jun 07 '11 at 14:06
  • Well, that's weird. Maybe you're using Java 5 instead of Java 6, where wildcards are actually supported. Can you confirm that as well? – Vineet Reynolds Jun 07 '11 at 14:33
  • Ah, then you'll need to avoid using wildcards. You'll have to specify all the JARs in the lib directory, in the classpath entry. Or, move to Sun/Oracle Java 6. – Vineet Reynolds Jun 07 '11 at 14:49
  • None other than the three I've mentioned - using a manifest, using Java 6, or using expanded paths in the classpath. You could write a script to perform the last option, if you wish to do so. – Vineet Reynolds Jun 07 '11 at 15:00
  • could you please throw me some example – user234194 Jun 07 '11 at 15:06
  • Well, I don't know a lot about Windows Powershell or scripting; more proficient in bash ;-). I think you should raise another question on SO. If you want a start, see [this script](http://gallery.technet.microsoft.com/scriptcenter/e789e7f7-41ef-4314-977c-51d98bde5234) I found by googling. It was quite tough to find a script to write the contents of that array into a file. – Vineet Reynolds Jun 07 '11 at 15:16
2

Use -cp (or -classpath) instead of -jar:

java -cp ../dist/MyProject.jar;../lib/*.jar com.my.test.MainTest

The -jar option is used for running a .jar file, which requires that the .jar file must include a manifest saying which class to execute. But here you don't want that, because you are already supplying the class to run (com.my.test.MainTest).

Update:

As @Rob mentions, the way to use wildcards in the classpath is just '*', not '*.jar', so you really want:

java -cp ../dist/MyProject.jar;../lib/* com.my.test.MainTest
Todd Owen
  • 14,034
  • 7
  • 46
  • 50
2
  1. Use -cp to specify the classpath
  2. Remove the .jar extension for the wildcard

Your resulting command would be:

    java -cp ..\dist\MyProject.jar;..\lib\* com.my.test.MainTest

Related question with enlightening answers.

Community
  • 1
  • 1
Rob Hruska
  • 111,282
  • 28
  • 160
  • 186