[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

starting java application with windows

Posted on 2005-05-17
51
Medium Priority
?
268 Views
Last Modified: 2010-03-31
Hi experts
I have a java application on my notebook.I want this application to start when i boot into windows automatically.
How do i achieve this ? Do i write some kind of batch file ? if so how ?or what are the alternatives?
0
Comment
Question by:SimpleJava
  • 26
  • 24
51 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 14020104
Yes you can start the batch file with the Win scheduler. Set it to run at startup

@echo off
java -classpath xxxx YourApp
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 14020113
Or you can just choose a command line of the second line and omit a batch file altogether
0
 

Author Comment

by:SimpleJava
ID: 14020204
CEHJ can u clarify the following:
u mean YourApp is my main class file.what if there are other class files that the main class calls?
how do i start the batch file with the Win scheduler ?
0
Independent Software Vendors: 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 86

Expert Comment

by:CEHJ
ID: 14020234
>>u mean YourApp is my main class file.what if there are other class files that the main class calls?

That doesn't matter. Just imagine you'd typed java X at the command line

>>how do i start the batch file with the Win scheduler ?

Keep it simple at first - omit the batch file
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 14020272
Use the 'Scheduled Tasks' applet in Control Panel. Browse to java.exe and be sure to check 'open advanced properties' on the way so you can put in the classpath and class
0
 

Author Comment

by:SimpleJava
ID: 14020286
thank you but
>>keep it simple at first-omit the batch file
i dont get it.no batch file, which way then ?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 14020328
Not x.bat but java.exe
0
 

Author Comment

by:SimpleJava
ID: 14020359
am i getting you. u mean i save the file

java -classpath xxxx YourApp

as java.exe in a notepad or something and then ?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 14020428
No. Just browse to java.exe in the Scheduled Apps Control Panel applet. You put the classpath and class in later in the wizard
0
 

Author Comment

by:SimpleJava
ID: 14020506
i cant locate the Scheduled Apps Control Panel applet. am on winxp
0
 

Author Comment

by:SimpleJava
ID: 14020553
Are you refering to the scheduled task wizard ?
0
 
LVL 18

Expert Comment

by:mgcIT
ID: 14020575
It's actually called Scheduled Tasks, and alternatively you can go to Start > Programs > Accessories > System Tools > Scheduled Tasks..

Although I think it would be easier to create a jar file and just put that in your Programs > Startup folder.  A jar file is basically an executable file
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 14020577
Yes. Sorry i changed the name part way through (see what i said first)
0
 

Author Comment

by:SimpleJava
ID: 14020629
Thanks so which one are you advocating ?
A jar file (how to do that) or browsing and locating java.exe and putting the class file in that directory ?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 14020673
It's going to be quicker *not* to use a jar file unless you need one anyway

>>... putting the class file in that directory ?

You can leave the class file where it is
0
 

Author Comment

by:SimpleJava
ID: 14020734
thank you but it seems am not getting u atall.
i thought you said i put the class files in java.exe(which i thought was a directory but cant even locate it now).
let me tell you what i got so far if only am right
1. name java -classpath xxxx YourApp as java.exe
2. put this file in scheduled task
3. windows will then run this task when it starts

am right ? CEHJ
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 14020775
>> thought you said i put the class files in java.exe(which i thought was a directory but cant even locate it now).

No, the class file(s) remain(s) in the same place. java.exe is a program, namely the Java interpreter

All you do is browser to java.exe in Scheduled Tasks. Check  'Open advanced properties ...' and put the classpath and class in
0
 

Author Comment

by:SimpleJava
ID: 14020850
to be honest you are not clear to me
>>All you do is browser to java.exe in Scheduled Tasks. Check  'Open advanced properties ...' and put the classpath and class in

ur sentence seems not to be completed. where do i put the classpath and the class ?
to make things worse i cant locate the program java.exe
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 14020943
>>ur sentence seems not to be completed. where do i put the classpath and the class ?

It is complete, but 'browser' should be 'browse'

I'll look for the caption of the box where you put it - hang on...

>>to make things worse i cant locate the program java.exe

That's where it's been installed. If you do

echo %PATH%

at the command line you should see it (or its bin directory) in there
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 14020986
It's in the box marked 'Run:'

You put the classpath and path after the command, which in my case is:

"C:\Program Files\Java\jre1.5.0_02\bin\java.exe"

or you could have

"C:\Program Files\Java\jre1.5.0_02\bin\javaw.exe"

So that would be


"C:\Program Files\Java\jre1.5.0_02\bin\java.exe" -classpath xxxx YourApp
0
 

Author Comment

by:SimpleJava
ID: 14021071
Thank you. i was looking for the .exe extension
i'll be back in a minute
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 14021214
Windows stupidly hides file extensions by default so you may need to make them visible
0
 

Author Comment

by:SimpleJava
ID: 14021317
i'm running into some problems with windows.i am getting the following warnings:
the task name cannot contain the characters :,\,/,<,> etc
this is what i have in the text field of the scheduled task wizard

java -classpath C:\Programs\Users\Joe\markValues

where markValues is the main class
what is wrong ?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 14021347
No, you should have something more like:


"C:\Program Files\Java\jre1.5.0_02\bin\java.exe" -classpath C:\Programs\Users\Joe  markValues

in the Run: box
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 200 total points
ID: 14021356
But at this rate it may be quicker to save the following as a batch file and put it into your startup folder:


@echo off
"C:\Program Files\Java\jre1.5.0_02\bin\java.exe" -classpath C:\Programs\Users\Joe  markValues
0
 

Author Comment

by:SimpleJava
ID: 14021408
but if i put it in the run box how show will i be that windows will open the program anytime i start my notebook?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 14021465
Why not test it?
0
 

Author Comment

by:SimpleJava
ID: 14021483
Thanks a lot. i will test all possible solutions and let you know.
Keep tight.
0
 

Author Comment

by:SimpleJava
ID: 14021637
i put the lines
@echo off
"C:\Program Files\Java\jre1.5.0_02\bin\java.exe" -classpath C:\Programs\Users\Joe  markValues

in a notepad saved as mark.bat.
i restart my computer and the java application is not started.the command prompt pops up and exits without the application showing up.
0
 

Author Comment

by:SimpleJava
ID: 14021660
when i pause the batch file i get the error exception in thread "main" java.lang.NoClassDefFoundError
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 14021684
That suggests your classpath's wrong
0
 

Author Comment

by:SimpleJava
ID: 14021776
this error occurs only with the lines
@echo off
"C:\Program Files\Java\jre1.5.0_02\bin\java.exe" -classpath C:\Programs\Users\Joe\markValues

but if i put the lines
@echo off
java -classpath "C:\Program\Users\Joe\markValues"

the application which is a gui is loaded alright with the exception that images(.jpg) are not loaded at all.
am i reaching you?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 14021803
Yes, you of course need to put in the correct path to *your* java.exe, or none at all (as long as it's on your path - and it is)

>> ...images(.jpg) are not loaded at all.

How are your images loaded in your app?
0
 

Author Comment

by:SimpleJava
ID: 14021841
i do with the following code
Box box=Box.createHorizontalBox();
c.insets=new Insets(8,8,8,8);
box.add(new JLabel(new ImageIcon("my.gif")));
....
.....
panel.add(box,c);
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 200 total points
ID: 14021857
Try


box.add(new JLabel(new ImageIcon(getClass().getResource("my.gif"))));
0
 

Author Comment

by:SimpleJava
ID: 14021923
thank you very much.works fine but one more problem with a driver.
There is the error i get with java.sql.SQLException:java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

this does not happen when i do java markValues at the command prompt but only with the batch. how do i resolve this ?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 14022637
You need to put the driver jar in the classpath too. That includes the file and extension
0
 

Author Comment

by:SimpleJava
ID: 14026137
maybe i'm not able to set the classpath correctly.i however solve the problem by copying the mysql com directory with all its contents and putting it in the directory that holds my main class files.
This works perfectly fine.
CEHJ do u have any idea of avoiding the poping up of the command window ?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 14026155
>>i however solve the problem by copying the mysql com directory with all its contents and putting it in the directory that holds my main class files.

That's an unfortunate way of solving it ;-)

>> maybe i'm not able to set the classpath correctly

Possibly. What did you give as the classpath?

>>CEHJ do u have any idea of avoiding the poping up of the command window ?

Run javaw.exe instead of java.exe



0
 

Author Comment

by:SimpleJava
ID: 14026195
classpath:   C:\mysqljconnect\mysql-connector-java-3.1.7\mysql-connector-java-3.1.7-bin.jar
0
 

Author Comment

by:SimpleJava
ID: 14026202
i tried javaw.exe but still have the command window poping up.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 14026265
>> classpath:   C:\mysqljconnect\mysql-connector-java-3.1.7\mysql-connector-java-3.1.7-bin.jar

That should be *part* of the classpath, not the whole of it. Your own class should also be part of it

>> i tried javaw.exe but still have the command window poping up.

Do you have anything asking for input or otherwise pausing execution?
0
 

Author Comment

by:SimpleJava
ID: 14026300
>> That should be part of the classpath,.... Your own class should also be part
 my own class --- which ones (the ones i'm running ?)
am  setting the classpath as an environmental variable

>> Do you have anything asking for input or ....
the command window is blank. nothing seen
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 14026341
>>am  setting the classpath as an environmental variable

Your classpath (in the startup) should be at least:

-classpath  C:\mysqljconnect\mysql-connector-java-3.1.7\mysql-connector-java-3.1.7-bin.jar
;C:\Programs\Users\Joe\markValues

but if it gets much longer, then you'd be better putting it in a batch file
0
 

Author Comment

by:SimpleJava
ID: 14026385
but what is wrong if i set the connector as an environment variable since java bin classpath is already set as an environmental variable
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 14026457
>> but what is wrong if i set the connector as an environment variable

Nothing, as long as you put all the classpath in:

http://mindprod.com/jgloss/classpath.html

>>since java bin classpath is already set as an environmental variable

      
The java bin directory is nothing to do with the classpath (see above link)

0
 

Author Comment

by:SimpleJava
ID: 14026489
Thank you for the information.Useful i guess i will manage.
Any info on the poping up of the command window?
0
 

Author Comment

by:SimpleJava
ID: 14030079
o.k manage to solve the pop up of the command window problem.
thanks for ur help
0
 

Author Comment

by:SimpleJava
ID: 14030195
thanks CEHJ
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 14030862
OK :-) What was it with the popping up?
0
 

Author Comment

by:SimpleJava
ID: 14035251
sorry i've been away.
the problem i imagined was a way to close the batch window after executing the command. it ges like this

START java -classpath xxxx myclass
EXIT

everything works fine.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

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…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses
Course of the Month19 days, 17 hours left to enroll

872 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