setting classpath in .bat file

i am running my java application using a .bat file.
In the top of the file i set the classapath, Something like

set classpath=..\log4j-1.2.7.jar;.;..\mysql-connector-java-3.0.6-stable-bin.jar;..\conf.jar;..\datasocket.jar;..\photos.jar;..\jh.jar;..\jhall.jar;jhbasic.jar;jsearch.jar;..\thirdparty\xercesImpl.jar;..\thirdparty\xmlParserAPIs.jar;

But this doesn't work.
If i set the classpath in the environment variables it works fine. But it won't be feasible to set the classpath path in environment variables of every user. So I want to set them from a .bat wherein the user clicks on it  and paths are set.

Please Help
LVL 8
thomas908Asked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
girionisConnect With a Mentor Commented:
> Because this is a installer, running the file is to be done later on through a shortcut or something

How do you create the shortcut? If your installer creates the shortcut then you might as well make it use the classpath manually (i.e. by using the -classpath switch).
0
 
objectsCommented:
should work, is that the same set command you use outside the batch?

what error exactly are u getting?

other option is to use the -classpath option when starting the vm.
0
 
girionisCommented:
It should work as essentially the commands you execute in a batch file should work the same as in the command prompt. What I suggest you do is to check if you have the batch file in the right folder, since you use relative paths and not not asbolute ones. Make sure that from the folder where you run the batch file if you run this line:

set classpath=..\log4j-1.2.7.jar;.;..\mysql-connector-java-3.0.6-stable-bin.jar;..\conf.jar;
..\datasocket.jar;..\photos.jar;..\jh.jar;..\jhall.jar;jhbasic.jar;jsearch.jar;..\thirdparty\xercesImpl.jar;..\thirdparty\xmlParserAPIs.jar;

from the command prompt it will run correctly.
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
Mayank SAssociate Director - Product EngineeringCommented:
Which OS? Windows?
0
 
objectsCommented:
using relative paths in your classpath means you need to run your app from the correct directory.
so make sure your bat has a cd command before starting vm
0
 
girionisCommented:
If he is using a batch file and he is also using "set" for the classpath I'd assume it's Windows :)
0
 
thomas908Author Commented:
yes its windows
0
 
thomas908Author Commented:
setting classpath in etiher batch file or prompt doesn't work.
The applciaiton can't find the files. I have to set them in the control panel under
system under environment variables
0
 
objectsCommented:
> The applciaiton can't find the files.

what class specifically isn't it finding, and where is that class located?
0
 
girionisCommented:
> The applciaiton can't find the files. I have to set them in the control panel under
> system under environment variables

How do you set them? Just copy/paste the setting into the batch file and it should work.
0
 
thomas908Author Commented:
classes are in jar files.
the files which i mentioned in the classpath

set classpath=..\log4j-1.2.7.jar;.;..\mysql-connector-java-3.0.6-stable-bin.jar;..\conf.jar;
..\datasocket.jar;..\photos.jar;..\jh.jar;..\jhall.jar;jhbasic.jar;jsearch.jar;..\thirdparty\xercesImpl.jar;..\thirdparty\xmlParserAPIs.jar;


If i set this classpath in environment variables in system under control panel the
application works. If i don't that is specify this classpath in batch file or command prompt
I get the follwoing error

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/log4j/Logg
er
        at com.pack1.pack2.main.App.<clinit>(App.java:37)
0
 
girionisCommented:
Ok, where do you have your batch file and where your log4j-1.2.7.jar file? Can you post the *exact* locations?
0
 
thomas908Author Commented:
>>Just copy/paste the setting into the batch file and it should work

actually batch file is only used to install the application. So once the applciation is installed the
batch file terminates. But the user may run the application later on for which this classpath is needed.
0
 
thomas908Author Commented:
app123/src/classes/com/pack1/pack2/main/App.java (com,pack1, pack2, mainare packages)
app123/src/log4j-1.2.7.jar (here are other jar files as well)

0
 
objectsCommented:
> But the user may run the application later on for which this classpath is needed.

just setting it in the install batch will not be enough then, as that only remains for that shell.
0
 
objectsCommented:
is the batch being run from directory above the directory containing your log4j jar?
0
 
thomas908Author Commented:
>>just setting it in the install batch will not be enough then, as that only remains for that shell.

u are right. What do i do then?
One solution could be to set it in the environment variables in system in control panel.
But that is not feasible in this case. Any other solution where the classpath automatically gets set?
0
 
girionisCommented:
> But the user may run the application later on for which this classpath is needed.

Why don't you bundle all files with a zip file then (including thebatch) so they all unzipped in a specific folder for *all* installations? If not I would suggest use another product to install your java app like installAnywhere (http://www.zerog.com/products_ia.shtml). The thing with a batch file is that even if you set the classpath upon restarting the computer it will be lost (unless you manage to put it into the autoexec.bat somehow).
0
 
girionisCommented:
>One solution could be to set it in the environment variables in system in control panel.
>But that is not feasible in this case. Any other solution where the classpath automatically gets
>set?

Two I can think of.

1. Keep the batch file and make the user use it to run the application. By doing so you can pre-set all the classpath. Bundle all of them in a zip file so they are uinzipped in specific folders that the batch file knows about them.

2. Make a manifest file and put there all the relevant jar files you need. Then bundle all classes and jar files in a zip file (same as above suggestion). The user can run the jar file by double clicking on it.

0
 
thomas908Author Commented:
I am already using IzPack. But in this classpath is set in target in the shortcut. But because my classpath
is huge and number of characters allowed in target in shortcut are limited, complete classpath does not get
specified. Now i don't know where to set the classpath.
0
 
girionisCommented:
In my suggestion 1. after you set all the classpath you simply have to add a line in the batch file something like:

java mypackage.myclass.MyClass

and it will be run upon double click or by command prompt.
0
 
objectsConnect With a Mentor Commented:
Sounds like using your jars manifest is the go.
0
 
thomas908Author Commented:
>>In my suggestion 1. after you set all the classpath you simply have to add a line in the batch file something like:
java mypackage.myclass.MyClass
and it will be run upon double click or by command prompt.
Because this is a installer, running the file is to be done later on through a shortcut or something
0
 
girionisCommented:
http://java.sun.com/docs/books/tutorial/jar/basics/manifest.html have a look at the section "Download Extensions - version 1.2 only" to see how you can set the classpath by using a manifest.
0
 
dttriCommented:
>>actually batch file is only used to install the application. So once the applciation is installed the batch file terminates. But the user may run the application later on for which this classpath is needed.

Hello,
Have you try this: in your bat file, after you set classpath environment, you call your java program immediately? Because after you set the environment variable, its value gone if the console closed (You can try by setting PATH variable and view it, and then close the prompt, open another prompt and view the PATH value. You should see the change).
To set a value permanent, set it in your autoexec.bat.

Tri
0
 
girionisCommented:
> Have you try this: in your bat file, after you set classpath environment, you call your java program immediately?

That's what I suggested also but it's obviously not a solution.
0
 
WebstormConnect With a Mentor Commented:
Hi thomas908,

Your classpath is relative to the starting directory, check from which directory you start your program when you set the classpath in the environment variables.
0
 
girionisCommented:
:)
0
 
WebstormCommented:
:-)
0
 
objectsCommented:
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.