I am facing a very unusual problem. I have created an executable server jar file. I am able to run it using the java -jar server.jar from the same directory where server.jar file is lying. However when i go to any other directory from prompt and execute java -jar server.jar it says :
Exception in thread "main" java.util.zip.ZipException: The system cannot find the file specified at java.util.zip.ZipFile.open(Native Method) at java.util.zip.ZipFile.<init>(ZipFile.java:110) at java.util.jar.JarFile.<init>(JarFile.java:115) at java.util.jar.JarFile.<init>(JarFile.java:57) I have set the classpath as well in the prompt using the command set CLASSPATH=%CLASSPATH%;c:\server\server.jar; But still not working Pls Help
When using an executable jar the classpath is set inside the manifest file like: Main-Class: ressa.GestorDatos Class-Path: ./ lib/common.jar lib/mm.mysql-2.0.11-bin.jar lib/xmlParserAPIs.jar lib/xercesImpl.jar Remember to press enter at the end of the last line Note that the classpath is relative to the jar location This works, at least, in Windows 98
Thanks Eduard But for this assignment we dont need to refer to any other jar file in the classpath Let me explain u using an example: 1) i created a simple test program test.java which has a main method that out puts hello world This test.java is in package myprogs 2) I create a manifest file for it having value Main-Class: myprogram.test 3) i made an executable jar file for this as jar -cfm test.jar mymanifest myprograms\ 4) Now i have test.jar file which is excutable. 5) if this file is in c:\amit and i am also in same directory on commnand prompt, i can run java -jar test.jar that works
but when i go to any other folder on the command prompt and run the same command it gives the error java.util.zipexception which means that it is not able to fond the jar file. I setted this file in class path but not working.
Pls Help test.jar is just an example but same i am doing for running server.jar in this assignment Amit
Hi Gurpreet By changing the directory i mean changing the current working directory using cd c:\Program Files and not changing the location of the test.jar file. The jar file is still in c:\amit if the current working directory on the command prompt is the same as of the directory in which the test.jar file lying, then it works but if they are not the same then it give this error. Amit
Look at the contents of your jar file using for example: jar -tf <your-jar-name> Sample output: META-INF/ META-INF/MANIFEST.MF myprogram/test.class
If your test class name and package is not displayed properly that means you haven't created the jar file correctly. You can try with this command: jar -cfm test.jar mymanifest -C myprograms\ This should work if myprogam/test.class is inside myprograms folder
Hi, I got you. What so ever may be the directory whether you are in sub directory or you change the directory. Every time you shift your program from one place to another you are expected to change the classpath that means everytime you have to set the classpath for a reason that you are changing the directories (Whether sub directory or same directory). During run time JVM looks for the all associated files which are to be linked with each other. For this reason it is desirable that you change your classpath. I dont know which is your platform. So, I cant say which would be actual format of classpath. But if you are using windows then change your classpath w.r.t directory. THIS THING YOU HAVE TO DOCUMENT IN YOUR README.TXT FILE. OTHERWISE PROBLEM !!!
Thanks Eduard I have followed all the steps u have written. After creating the jar file i placed in c:\amit After that 1) i opened a command prompt and executed cd c:\amit 2) Then i set the classpath as: set CLASSPATH=%CLASSPATH%;c:\amit\test.jar 3) then i went to folder called nitin cd c:\nitin 4) i tried java -jar test.jar it gave me the follwing error Exception in thread "main" java.util.zip.ZipException: The system cannot find th e file specified at java.util.zip.ZipFile.open(Native Method) at java.util.zip.ZipFile.<init>(ZipFile.java:110) at java.util.jar.JarFile.<init>(JarFile.java:115) at java.util.jar.JarFile.<init>(JarFile.java:57) Again i changed the current working directory on the command prompt as: cd c:\amit and executed java -jar test.jar and it worked Thus problem is not resolved REMEMBER : the test.jar file is always in c:\amit The following lines are taken from javadoc section ************************************** PS: "When you use -jar option, jar file is the source of all user classes and other classpath settings are ignored". Found this comment in the java docs.. **************************************** Is this the reason ??? Pls Help Amit [ July 24, 2002: Message edited by: Amit Kr Kumar ]
I see know! I got confused! Sorry! The problem is not the classpath. The problem is that Java can't find the jar file. In fact, if all your classes are inside the jar file, then you don't even need a classpath I'm afraid the only way I know is using the full path like: java -jar c:\amid\test.jar If you omit the directory then java looks for the jar file in the current directory.
Yup u right it works that way But exam instruction says: ************************************* How to execute the programs. You must provide exact command-line instructions. If any environmental setup is required, do not just say what needs to be done; provide instructions on how to perform the setup. For example, do not say something like "add server.jar to your classpath". You should document exactly how to add the jar file to the classpath. Note: your program must run correctly no matter what directory it is installed in. ***************************************** Read the last line carefully How to meet this requirement in this case ??? How u started your server and client ?? Amit
I don't bother about that. In windows I just have to double click over the application jar file and it works. No need of environment variables like classpath. I'm sure other graphical OS work the same way. The command lines are very easy: java -jar <install-dir>/server.jar java -jar <install-dir>/client.jar They must provide the installation directory. This way my program works no matter the directory it has been installed in. However, advice from those who have already uploaded the assignment would be welcomed.