?
Solved

setting classpath in .bat file

Posted on 2004-08-08
30
Medium Priority
?
2,229 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:Mayank S
ID: 11750398
Which OS? Windows?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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 900 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 900 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 200 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
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 …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Suggested Courses
Course of the Month8 days, 7 hours left to enroll

616 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