change default working directory

M

malpropio

Using eclipse I know that you can change the working directory in the
run wizard for a particular class but I dont know how to do the same
thing from the console.
I'm trying to do this in linux because I'm creating a cron job and
my software needs to read a configpath that lives in the working
directory but that the cronjob dont seem to be able to read cause I
believe its reading another folder.
thank you
 
D

Donkey Hottie

Using eclipse I know that you can change the working directory in the
run wizard for a particular class but I dont know how to do the same
thing from the console.
I'm trying to do this in linux because I'm creating a cron job and
my software needs to read a configpath that lives in the working
directory but that the cronjob dont seem to be able to read cause I
believe its reading another folder.
thank you

Afaik Java does not have chdir() capability, but the current directory is
what happened to be the directory when java was starter.

In a cron job, you can cd to the wanted directory using linux command cd
before you launch java

--------- mycronjob.sh ---------------
#!/bin/sh

cd /home/malpropio/workdir
java -jar myapp.jar

--------------------------------------
 
M

malpropio

great solution
massive thank you

p.s: still find it weird how eclipse achieve it.
 
D

Donkey Hottie

great solution
massive thank you

p.s: still find it weird how eclipse achieve it.

No idea. One possibility is that is spawns a new JVM with a different
directory. That is how I have seen it done in ant-tasks.
 
M

Mike Schilling

Donkey said:
(e-mail address removed):


Afaik Java does not have chdir() capability,

You can change the current directory by setting the system property
"user.dir". For example

import java.io.*;

public class Cd
{
public static void main(String[] args) throws Exception
{
String dir = System.getProperty("user.dir");
System.out.println(dir);
File cur = new File(".");
System.out.println(cur.getCanonicalPath());
System.setProperty("user.dir", "C:\\temp");
cur = new File(".");
System.out.println(cur.getCanonicalPath());

}
}

outputs

c:\java
C:\java
C:\temp
In a cron job, you can cd to the wanted directory using linux command
cd before you launch java

--------- mycronjob.sh ---------------
#!/bin/sh

cd /home/malpropio/workdir
java -jar myapp.jar

--------------------------------------

But that's a simpler and thus better solution.
 
M

Mike Schilling

Eric said:
Mike said:
Donkey said:
(e-mail address removed):

Using eclipse I know that you can change the working directory in
the run wizard for a particular class but I dont know how to do the
same thing from the console.
I'm trying to do this in linux because I'm creating a cron job
and my software needs to read a configpath that lives in the
working directory but that the cronjob dont seem to be able to
read cause I believe its reading another folder.
thank you

Afaik Java does not have chdir() capability,

You can change the current directory by setting the system property
"user.dir". For example

import java.io.*;

public class Cd
{
public static void main(String[] args) throws Exception
{
String dir = System.getProperty("user.dir");
System.out.println(dir);
File cur = new File(".");
System.out.println(cur.getCanonicalPath());
System.setProperty("user.dir", "C:\\temp");
cur = new File(".");
System.out.println(cur.getCanonicalPath());

}
}

outputs

c:\java
C:\java
C:\temp

This "works" in the sense that it produces the output shown,
but fails to actually change the working directory. For example,
if you follow the above with

new File("foo.bar").createNewFile();

(and provide for the IOException), you'll find that the new file
appears in c:\java rather than in c:\temp.

Right you are. Never mind.
 
M

Mark Space

Donkey said:
--------- mycronjob.sh ---------------
#!/bin/sh

cd /home/malpropio/workdir
java -jar myapp.jar

--------------------------------------

I would be tempted to pass the path into the jar on the command line:

java -jar myapp.jar /home/malpropio/workdir

This seems more general than required to cd to the desired directory.
Of course you'll be required to modify the jar file. Just my 2 nickels.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,781
Messages
2,569,615
Members
45,303
Latest member
Ketonara

Latest Threads

Top