Solved

setting classpath in .bat file

Posted on 2004-08-08
30
1,925 Views
Last Modified: 2012-06-27
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
0
Comment
Question by:thomas908
  • 11
  • 8
  • 7
  • +3
30 Comments
 
LVL 92

Expert Comment

by:objects
ID: 11750391
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
 
LVL 35

Expert Comment

by:girionis
ID: 11750394
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
 
LVL 30

Expert Comment

by:mayankeagle
ID: 11750398
Which OS? Windows?
0
 
LVL 92

Expert Comment

by:objects
ID: 11750409
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
 
LVL 35

Expert Comment

by:girionis
ID: 11750414
If he is using a batch file and he is also using "set" for the classpath I'd assume it's Windows :)
0
 
LVL 8

Author Comment

by:thomas908
ID: 11750427
yes its windows
0
 
LVL 8

Author Comment

by:thomas908
ID: 11750431
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
 
LVL 92

Expert Comment

by:objects
ID: 11750442
> The applciaiton can't find the files.

what class specifically isn't it finding, and where is that class located?
0
 
LVL 35

Expert Comment

by:girionis
ID: 11750459
> 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
 
LVL 8

Author Comment

by:thomas908
ID: 11750567
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
 
LVL 35

Expert Comment

by:girionis
ID: 11750580
Ok, where do you have your batch file and where your log4j-1.2.7.jar file? Can you post the *exact* locations?
0
 
LVL 8

Author Comment

by:thomas908
ID: 11750585
>>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
 
LVL 8

Author Comment

by:thomas908
ID: 11750600
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
 
LVL 92

Expert Comment

by:objects
ID: 11750601
> 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
 
LVL 92

Expert Comment

by:objects
ID: 11750606
is the batch being run from directory above the directory containing your log4j jar?
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 8

Author Comment

by:thomas908
ID: 11750609
>>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
 
LVL 35

Expert Comment

by:girionis
ID: 11750610
> 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
 
LVL 35

Expert Comment

by:girionis
ID: 11750622
>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
 
LVL 8

Author Comment

by:thomas908
ID: 11750628
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
 
LVL 35

Expert Comment

by:girionis
ID: 11750639
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
 
LVL 92

Assisted Solution

by:objects
objects earned 225 total points
ID: 11750641
Sounds like using your jars manifest is the go.
0
 
LVL 8

Author Comment

by:thomas908
ID: 11750683
>>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
 
LVL 35

Expert Comment

by:girionis
ID: 11750687
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
 
LVL 7

Expert Comment

by:dttri
ID: 11750696
>>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
 
LVL 35

Accepted Solution

by:
girionis earned 225 total points
ID: 11750697
> 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
 
LVL 35

Expert Comment

by:girionis
ID: 11750704
> 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
 
LVL 13

Assisted Solution

by:Webstorm
Webstorm earned 50 total points
ID: 11750776
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
 
LVL 35

Expert Comment

by:girionis
ID: 11750982
:)
0
 
LVL 13

Expert Comment

by:Webstorm
ID: 11750988
:-)
0
 
LVL 92

Expert Comment

by:objects
ID: 11758781
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This video teaches viewers about errors in exception handling.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now