Link to home
Start Free TrialLog in
Avatar of SimpleJava
SimpleJava

asked on

starting java application with windows

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?
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Yes you can start the batch file with the Win scheduler. Set it to run at startup

@echo off
java -classpath xxxx YourApp
Or you can just choose a command line of the second line and omit a batch file altogether
Avatar of SimpleJava
SimpleJava

ASKER

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 ?
>>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
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
thank you but
>>keep it simple at first-omit the batch file
i dont get it.no batch file, which way then ?
Not x.bat but java.exe
am i getting you. u mean i save the file

java -classpath xxxx YourApp

as java.exe in a notepad or something and then ?
No. Just browse to java.exe in the Scheduled Apps Control Panel applet. You put the classpath and class in later in the wizard
i cant locate the Scheduled Apps Control Panel applet. am on winxp
Are you refering to the scheduled task wizard ?
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
Yes. Sorry i changed the name part way through (see what i said first)
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 ?
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
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
>> 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
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
>>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
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
Thank you. i was looking for the .exe extension
i'll be back in a minute
Windows stupidly hides file extensions by default so you may need to make them visible
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 ?
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
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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?
Why not test it?
Thanks a lot. i will test all possible solutions and let you know.
Keep tight.
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.
when i pause the batch file i get the error exception in thread "main" java.lang.NoClassDefFoundError
That suggests your classpath's wrong
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?
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?
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);
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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 ?
You need to put the driver jar in the classpath too. That includes the file and extension
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 ?
>>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



classpath:   C:\mysqljconnect\mysql-connector-java-3.1.7\mysql-connector-java-3.1.7-bin.jar
i tried javaw.exe but still have the command window poping up.
>> 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?
>> 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
>>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
but what is wrong if i set the connector as an environment variable since java bin classpath is already set as an environmental variable
>> 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)

Thank you for the information.Useful i guess i will manage.
Any info on the poping up of the command window?
o.k manage to solve the pop up of the command window problem.
thanks for ur help
thanks CEHJ
OK :-) What was it with the popping up?
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.