• Post Reply Bookmark Topic Watch Topic
  • New Topic

got stucked on executing a .sh file in java which has been written by java program  RSS feed

 
Rahul Shilpakar
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
package HX;
import java.net.URL;



public void convert() {
//URL u;
BufferedInputStream in;
FileOutputStream out;

Tidy tidy = new Tidy();

//Tell Tidy to convert HTML to XML
tidy.setXmlOut(true);

try {
//Set file for error messages
//tidy.setErrout(new PrintWriter(new FileWriter(errOutFileName), true));
//u = new URL(url);


//Create input and output streams

in = new BufferedInputStream(new DataInputStream(new FileInputStream ("////home//rakesh//stuff//index.html" )));
out = new FileOutputStream("////home//rakesh//stuff//mn.xml");

//Convert files
System.out.println("111111111111111");
tidy.parse(in, out);
System.out.println("555555555");

//Clean up
in.close();
out.close();

} catch (IOException e) {
System.out.println(this.toString() + e.toString());
}
}

public static void main(String[] args) throws IOException {

TestHTML2XML_1 t = new TestHTML2XML_1();
t.convert();

File f=new File("////home//rakesh//stuff//myshell.sh");
FileOutputStream fos=new FileOutputStream(f);
OutputStreamWriter os=new OutputStreamWriter(fos);
os.write("cd /home");
os.write("cd rakesh");
os.write("cd stuff");

os.write("tidy -asxhtml -numeric ///home//rakesh//stuff//index.html //home//rakesh//stuff//index.xml");
os.close();
fos.close();

Runtime rt=Runtime.getRuntime();

Process p=rt.exec("/myshell.sh");
System.out.println("rrrr");
}
}
----------------------
1) the class converts HTML file to XML

1) scenario is that am writing some commands in .sh file through java code and close it.

2) now if i give the command on command line (which is shown in bold) its generating XML....No problem..

3) but i waaant to run that command from java code itself (as shown in ITALICS. so i write that command in .sh file so as ill execute that .sh file from with in the programm insted from command prompt. and as soon as execute method ends i am expecting my XML to be generated there.

Please help me...........I AM ON GUN POINT.........
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For starters, "/myshell.sh" implies that the script lives in the root directory, which is not where you're creating it. Use the correct path to refer to it.

You may need to handle the various streams associated with the script; see this article for that and other pitfalls to avoid when using Runtime.exec.
 
Rahul Shilpakar
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks somebody replied..
still the problem persists..

i gave the full path of (.sh) file in execute method. but it still doesn't work...

e.g. Runtime rt = Runtime.getRuntime();
Process p = rt.exec("////home//rahul//myshell.sh");

the error is something like this ::: //..//...//myshell.sh can't execute
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does the process that executes this have rights to all the directories and files?

BTW, why are you doubling all slashes?
 
Rob Spoor
Sheriff
Posts: 20904
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ulf Dittmer:
BTW, why are you doubling all slashes?

Yeah, why? You only need to double backslashes because \ is the Java escape character. Therefore, the backslash character is represented as \\. The forward slash is just / though.
 
Rahul Shilpakar
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let me correct our confusion please....

putting double forward slash works doesn't give problem.
Its creating files which are mentioned with full path like this (////home//rahul//....).

Ya it works, Infact it is making (.sh) file in respective folder also.
Also the command which i writing in that .sh also got written very well in it.
There is only one command in it.

:roll: the problem is that is not executing it.

i think we need kind of R&D here..
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So what's the answer to my question? It's important to be sure of that.
 
Rahul Shilpakar
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
YES, ulf

i have cheked all the permissions to each file and folder.

 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24215
37
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The shell script you're writing is just a plain file; you haven't marked it executable, so Runtime.exec() will fail to execute it. Either explicitly feed it to the shell to be executed -- i.e., exec("sh /home/...") -- or make it executable before running it -i.e.,

Runtime.getRuntime().exec("chmod +x /home/...");
Runtime.getRuntime().exec("/home...");
 
Rahul Shilpakar
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what is x in your above code? what does it do?
 
Freddy Wong
Ranch Hand
Posts: 959
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
chmod +x means giving executable permission to a file.
 
Rahul Shilpakar
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does x means any synonym for anything its directly a command?
 
Rob Spoor
Sheriff
Posts: 20904
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
x is hardcoded and means "execute rights".

There are three main type of rights on Linux and Unix:
"r" gives read permissions
"w" gives write permissions
"x" gives execute permissions

With chmod, you can grant (using +, e.g. "chmod +r +w" or shorter "chmod +rw") or revoke (using -, e.g. "chmod -r -w -x" or shorter "chmod -rwx") those rights.

You can also specify these rights for only the user, the group or everyone, but you can find more info on that using "man chmod" on your Linux / Unix machine.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!