LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > General
User Name
Password
General This forum is for non-technical general discussion which can include both Linux and non-Linux topics. Have fun!

Notices

Reply
 
Search this Thread
Old 01-30-2006, 05:50 AM   #1
leamassiot
Member
 
Registered: Dec 2005
Location: France
Distribution: Debian
Posts: 123

Rep: Reputation: 17
Execute a windows command from inside a c-cgi-script


Hi all,

I've a problem I do not manage to solve. I've been searching a lot on the web but I haven't found the answer to my problem. Please help.

I want to execute a Windows program ("javac" for instance) from inside a c-cgi-script.
Let us say the c-cgi-script is "foo.cgi".
I want "foo.cgi" to execute the "javac MyClass.java" command (for instance).

Do you know how to do that?

Notice that I've tried to use:
- the "CreateProcess" (Winbase.h) function,
- and also the "system" function
but got no significant results.

Any ideas, solutions?

Thanks in advance,

--
Léa Massiot
 
Old 01-30-2006, 08:05 AM   #2
leamassiot
Member
 
Registered: Dec 2005
Location: France
Distribution: Debian
Posts: 123

Original Poster
Rep: Reputation: 17
I think my problem is related to the "environment" (set of environment variables) I can recover or not within the c-cgi-script.

Let me explain:

Consider simply the following c program:

Code:
#include <stdlib.h>
#include <stdio.h>

int main()
{  
  char * path;
  char * classpath;
  char * java_home;

  path = getenv("PATH");
  classpath = getenv("CLASSPATH");
  java_home = getenv("JAVA_HOME");

  printf("\nPATH = %s\n\n", path);
  printf("CLASSPATH = %s\n\n", classpath);
  printf("JAVA_HOME = %s\n", java_home);

  return 0;
}
A/ When you execute it in a MSDOS console, you get what you expect:
the "Path", "CLASSPATH" and "JAVA_HOME" environment variables values.

B/ But when you make a c-cgi-script based on the program above and execute it:
you recover the "Path" variable value but the "CLASSPATH" and "JAVA_HOME are "null".

=> This is the problem I have.
=> Is it possible to recover those values? I really need them

Thanks for reading and helping.

--
Léa Massiot
 
Old 01-30-2006, 08:36 AM   #3
leamassiot
Member
 
Registered: Dec 2005
Location: France
Distribution: Debian
Posts: 123

Original Poster
Rep: Reputation: 17
Really, I do not understand:
- I can recover some values: "Path", "SystemRoot".
- but I cannot recover the "CLASSPATH", "JAVA_HOME", "PROCESSOR_IDENTIFIER" values (they are null).



--
Léa Massiot
 
Old 01-30-2006, 09:03 AM   #4
leamassiot
Member
 
Registered: Dec 2005
Location: France
Distribution: Debian
Posts: 123

Original Poster
Rep: Reputation: 17
When I try to recover my environment variables in my c-cgi-script all their values are (null) except for:
- ComSpec
- Path
- SystemRoot
- windir

Oops oops
I'll search in that direction.

--
Léa Massiot
 
Old 01-30-2006, 09:46 AM   #5
leamassiot
Member
 
Registered: Dec 2005
Location: France
Distribution: Debian
Posts: 123

Original Poster
Rep: Reputation: 17
CGI environment variables are not Operating System environment variables . They are maintained internally by CGI programs and some other web aplication programs.

This is the reason why I do not manage to recover most of my so called "environment variables" - OS environment variables (OS varenvs) in fact - using getenv().

I could write my OS varenvs in a file and read that file but it is not really clean, isn't it? No ideas?

--
Léa Massiot
 
Old 01-30-2006, 11:01 AM   #6
timmeke
Senior Member
 
Registered: Nov 2005
Location: Belgium
Distribution: Red Hat, Fedora
Posts: 1,515

Rep: Reputation: 61
First of all, this is a linux forum, not a windows one.

Secondly, why try writing your own C program for this? Many languages, like PhP for one, already do this for you.
You only need to know what languages are supported on the webserver you're writing your programs for.

Thirdly, there are 2 kinds of variables:
-CGI variables, passed usually via Get and Post methods on websites
-environment variables
On windows, the latter are split up in "user environment variables" and "system environment variables".

It is however likely that your webserver is running in it's own environment - ie using it's own environment variables. Your DOS box on the other hand has your environment. That's why you can't see the variables when you try running your application via the browser.
 
Old 01-30-2006, 12:46 PM   #7
leamassiot
Member
 
Registered: Dec 2005
Location: France
Distribution: Debian
Posts: 123

Original Poster
Rep: Reputation: 17
Firstly - Well, no need to upset me more than that: I hate "Windaube" and I wish I could avoid working with it, but I can't.

Secondly - This forum is the only one I've subscribed to and I do not intend to subscribe to any other one .

Thirdly - Even if my problem is a "Windaube" one, it is still interesting, especially when you are living in the real world (in which you are obliged to work with "Windaube", yes yes). Moreover, as far as know (I'm also a linuxian) there also are environment variables under *nix which really look like "Windaube" ones: "CLASSPATH", etc. are not exclusively "Windaube" varenvs . My problem applies more or less to *nix: I dare say so. If you simply knew a little bit about "Windaube" you would recognize that there are a lot of things in common.

Fourthly - I hate Ayatollahs.
=> No need to learn me things I already know.
=> No need tell me that I should go and see somewhere else...
=> Thanks in advance.

Fifthly - If you want to be cop just go, be a cop, there is no stupid job, only stupid persons.

Well, honestly spare us such remarks .

--
Léa Massiot
 
Old 01-30-2006, 02:21 PM   #8
rshaw
Senior Member
 
Registered: Apr 2001
Location: Perry, Iowa
Distribution: Mepis , Debian
Posts: 2,694

Rep: Reputation: 45
moving to general.
 
Old 01-31-2006, 03:03 AM   #9
timmeke
Senior Member
 
Registered: Nov 2005
Location: Belgium
Distribution: Red Hat, Fedora
Posts: 1,515

Rep: Reputation: 61
Quote:
Originally Posted by leamassiot
Firstly - Well, no need to upset me more than that: I hate "Windaube" and I wish I could avoid working with it, but I can't.
Sorry if my remarks upset you. That was not my intention. As for working with M$ Win, well, that makes 2 of us.

Quote:
Originally Posted by leamassiot
Secondly - This forum is the only one I've subscribed to and I do not intend to subscribe to any other one .
I agree that this is by far one of the greatest forums I've ever seen. Beats the crap out of the M$ forums at least.

Quote:
Originally Posted by leamassiot
Thirdly - Even if my problem is a "Windaube" one, it is still interesting, especially when you are living in the real world (in which you are obliged to work with "Windaube", yes yes). Moreover, as far as know (I'm also a linuxian) there also are environment variables under *nix which really look like "Windaube" ones: "CLASSPATH", etc. are not exclusively "Windaube" varenvs . My problem applies more or less to *nix: I dare say so. If you simply knew a little bit about "Windaube" you would recognize that there are a lot of things in common.
You're right. Environment variables do apply to *nix too. Only *nix handles them better and doesn't try to hide them somewhere in the middle of a gazilion other settings.
As for CLASSPATH and stuff. Those are typical Java environment variables and are thus platform independent (that's the whole purpose of Java in the first place).

In order for me to help you further, you'll need to share some configuration information. For instance, what web server are you using (Apache I hope)? How is it started? Is it a background service (as it should be) or does it run via the Scheduler?

http://www.jsifaq.com/SUBP/TIP7800/rh7868.htm
may be relevant.
 
Old 01-31-2006, 04:35 AM   #10
leamassiot
Member
 
Registered: Dec 2005
Location: France
Distribution: Debian
Posts: 123

Original Poster
Rep: Reputation: 17
Hi mister Timmeke,
Hi Others,

- Well it's ok, sorry too for irritating myself.

- Thank you for suggesting another solution with PHP. I know it is possible but I would like to make my solution work first - if it is possible(?). If I have no other choice I'll consider that alternative. Understand that I have been already working a lot on my solution and I think I'm not so far from solving it. Thanks anyway.

- Something that works, something that doesn't work:
By initializing myself in the c-cgi-script the "CLASSPATH" variable doing something like:

Code:
char CLASSPATH[1000] = "my classpath....";
where "my classpath...." is the value of the OS "CLASSPATH" varenv,
i managed to execute the following "javac" command:

Code:
javac MyClass.java
which produced - as expected - the "MyClass.class" class.

However, I do not manage to run the

Code:
java MyClass
command inside my c-cgi-script.

This is the problem I have to work on.

- To answer partly some of your questions:
o I use Apache via EasyPHP.
o For the time being, I start it manually at the beginning of my session.

- Well, listen, I'm sorry for having been nasty but things have to be clear. I do not like M$ Win nor Java especially but have to work with them. That's all: it is not delightful.

- I'll work on my problem today again and write my solution on the forum if I find one.

Good day and thanks for the time you spend for me ,
sincerely yours,

--
Léa Massiot
 
Old 01-31-2006, 05:22 AM   #11
timmeke
Senior Member
 
Registered: Nov 2005
Location: Belgium
Distribution: Red Hat, Fedora
Posts: 1,515

Rep: Reputation: 61
Quote:
Originally Posted by leamassiot
- Well it's ok, sorry too for irritating myself.
No problem. Happens to all of us.

Quote:
Originally Posted by leamassiot
- Thank you for suggesting another solution with PHP. I know it is possible but I would like to make my solution work first - if it is possible(?). If I have no other choice I'll consider that alternative. Understand that I have been already working a lot on my solution and I think I'm not so far from solving it.
OK. I see.

Quote:
Originally Posted by leamassiot
However, I do not manage to run the
Code:
java MyClass
command inside my c-cgi-script.
Strange indeed, since the javac en java binaries are in the same directory. Can you give a small clue on what your Java program is supposed to do?
Have you also taken a look at the Apache logs?

Quote:
Originally Posted by leamassiot
o I use Apache via EasyPHP.
o For the time being, I start it manually at the beginning of my session.
How do you start it? Via Config Panel - Admin tools - Services? Via a shortcut on your desktop?
If it's a shortcut, you could create a shortcut to a .bat file instead that sets your environment vars prior to launching Apache, for instance. But if it runs as a service, the environment is a completely different story, as pointed out in the jsifaq webpage.

Quote:
Originally Posted by leamassiot
- Well, listen, I'm sorry for having been nasty but things have to be clear. I do not like M$ Win nor Java especially but have to work with them. That's all: it is not delightful.
We all have to do some jobs we don't like and get frustrated by them. That's life.

Quote:
Originally Posted by leamassiot
Good day and thanks for the time you spend for me
Good day to you too, leamassiot and you're welcome.
Don't hesitate to post again if you have additional questions.
 
Old 01-31-2006, 05:28 AM   #12
leamassiot
Member
 
Registered: Dec 2005
Location: France
Distribution: Debian
Posts: 123

Original Poster
Rep: Reputation: 17
Hi (bis),

A/ I manage to execute:

Code:
C:\\Program Files\\Internet Explorer\\IEXPLORE.EXE
with "CreateProcess".

Sorry, it is "Internet Explorer" .

B/ But "java HelloWorldApp" still fails.


Code:
HelloWorldApp.java

class HelloWorldApp 
{
  public static void main(String[] args)
  {
    //System.out.println("Content-type:text/html");
    //System.out.println("13101310");      
    System.out.println("Hello World!"); //Display the string.
  }
}

C/
a) My Apache "error.log" file says (on the same line):

Quote:
malformed header from script. Bad header=Hello World!:
c:/program files/easyphp1-8/apache/cgi_bin/prog.cgi

b) If I uncomment the two lines above, my Apache "error.log" file says (on the same line):

Quote:
malformed header from script. Bad header=13101310:
c:/program files/easyphp1-8/apache/cgi_bin/prog.cgi
D/ Thanks for moving my thread to "General".
Sorry for the mistake.


--
Léa Massiot
 
Old 01-31-2006, 05:42 AM   #13
leamassiot
Member
 
Registered: Dec 2005
Location: France
Distribution: Debian
Posts: 123

Original Poster
Rep: Reputation: 17
o Java program & Apache "error.log" file
Quote:
Strange indeed, since the javac en java binaries are in the same directory. Can you give a small clue on what your Java program is supposed to do?
Have you also taken a look at the Apache logs?
I was doing it when you replied to me.
Is it enough?

o Starting Apache & jsifaq
Quote:
How do you start it? Via Config Panel - Admin tools - Services? Via a shortcut on your desktop?
If it's a shortcut, you could create a shortcut to a .bat file instead that sets your environment vars prior to launching Apache, for instance. But if it runs as a service, the environment is a completely different story, as pointed out in the jsifaq webpage.
(Shame on me) via a shortcut. More precisely:
Start -> Programs -> EasyPHP -> EasyPHP
I'll look at the link (jsifaq) you suggested me. Thanks a lot. See you.

o I'm a bit ashamed about my excessive reaction . Forgive me.

--
Léa Massiot
 
Old 01-31-2006, 06:38 AM   #14
leamassiot
Member
 
Registered: Dec 2005
Location: France
Distribution: Debian
Posts: 123

Original Poster
Rep: Reputation: 17
A/ Something that works
The following "java" program (no need to read it, it works) simply:
o creates a new file ("myfile.txt"),
o and writes "hello world!" in this file.

Code:
HelloWorldApp.java

import java.io.*;

public class HelloWorldApp 
{
  // Main method
  public static void main (String args[])
  {
    // Stream to write file
    FileOutputStream fout;		

    try
    {
    // Open an output stream
    fout = new FileOutputStream ("myfile.txt");

    // Print a line of text
    new PrintStream(fout).println ("hello world!");

    // Close our output stream
    fout.close();		
    }
    // Catches any error conditions
    catch (IOException e)
    {
    System.err.println ("Unable to write to file");
    System.exit(-1);
    }
  }	
}
When I ask my c-cgi-script to execute "java HelloWorldApp", well, it works. Fine, no?

Conclusion: there is nothing strange as you pointed it out... "java" works as well as "javac" does. The problem is somewhere else .

B/ Something that fails
Now still a problem persists (this was why I was trying to make sure "java" worked well):

I want my c-cgi-script to execute the following command:

Code:
fop -xml catalogue.xml -xsl catalogue2fo.xsl -pdf catalogue.pdf
If you have a look at "fop.bat" (~ "fop.sh") it merely consists in:
o initializing "LOCALCLASSPATH",
o executing "java".

Code:
rem @ECHO OFF

rem %~dp0 is the expanded pathname of the current script under NT
set LOCAL_FOP_HOME=
if "%OS%"=="Windows_NT" set LOCAL_FOP_HOME=%~dp0

set LIBDIR=%LOCAL_FOP_HOME%lib
set LOCALCLASSPATH=%LOCAL_FOP_HOME%build\fop.jar
set LOCALCLASSPATH=%LOCALCLASSPATH%;%LIBDIR%\xml-apis.jar
set LOCALCLASSPATH=%LOCALCLASSPATH%;%LIBDIR%\xercesImpl-2.2.1.jar
set LOCALCLASSPATH=%LOCALCLASSPATH%;%LIBDIR%\xalan-2.4.1.jar
set LOCALCLASSPATH=%LOCALCLASSPATH%;%LIBDIR%\batik.jar
set LOCALCLASSPATH=%LOCALCLASSPATH%;%LIBDIR%\avalon-framework-cvs-20020806.jar
set LOCALCLASSPATH=%LOCALCLASSPATH%;%LIBDIR%\jimi-1.0.jar
set LOCALCLASSPATH=%LOCALCLASSPATH%;%LIBDIR%\jai_core.jar
set LOCALCLASSPATH=%LOCALCLASSPATH%;%LIBDIR%\jai_codec.jar

java -cp "%LOCALCLASSPATH%" org.apache.fop.apps.Fop %1 %2 %3 %4 %5 %6 %7 %8
The command fails to execute ("CreateProcess" fails with error 2: "ERROR_FILE_NOT_FOUND").

Notice that the two files: "catalogue.xml" and "catalogue2fo.xsl" are in the same directory as the c-cgi-script. I do not understand which file it cannot find.

Thanks so much for reading, if it is too large tell me, please .

--
Léa Massiot
 
Old 01-31-2006, 07:58 AM   #15
timmeke
Senior Member
 
Registered: Nov 2005
Location: Belgium
Distribution: Red Hat, Fedora
Posts: 1,515

Rep: Reputation: 61
No, the more info I get, the better.
Don't be ashamed of your reactions.
You're very friendly now and that's the ideal way to get lots of help, especially on forums like this one!


Actually, your Apache logs are quite helpful. The fact that you can launch a Java app that writes to a file rather than to your browser, proves that your environment is good enough for compiling/running Java applications.

Quote:
malformed header from script.
This tells me that the HTTP header sent by your program to the browser isn't OK.
The first malformed header error tells you that there's something wrong with using "HelloWorld!" as HTTP header. Then you've tried adding the content-type stuff. You were right to add that line. Browsers expect to get the HTTP header first and this header should always start by claiming what kind of document you are sending to the browser, if I'm not mistaken. In this case, a plain text/html document is indeed the type you want. But, and this is mistake that I've made myself a while back, HTTP headers need to end with a double
\n. This is enforced by an RFC.
So, instead of just doing
Code:
System.out.println("Content-type:text/html");
you should print something like:
Code:
System.out.println("Content-type:text/html\n\n");
However, that's where things get kind of complicated. On some platforms, \n is actually translated to \n\r and stuff. You may want to check if Java does this kind of translation on Windows platforms.

So, what I propose is that you simply try adding the \n\n first. If that doesn't work, you should try replacing the \n characters with their ASCII representations (I think there character 12 or 13 or something).
In any case, your HTTP header must end with 2 times the ASCII value of \n (newline).

Note also that you can put other HTTP headers as well, like the length of the document (content-length) that the browser is about to receive, but most CGI programs only send the "content-type" line for simplicity.

As far as I know, and you seem to know a lot more about it than I, your fop.bat script seems to be OK.
However, I don't know if Apache can actually execute .bat files. And maybe the "file not found" comes from the fact that Apache can't find your .bat file, but that's just a guess. Or maybe it can't find some system file needed for running .bat files.
In any case, when I was talking about the .bat file, I was trying to say that your shortcut should launch the .bat file, that sets up your Java environment first and then launches Apache via EasyPhP (instead of just launching EasyPhP). I didn't mean to say that your .bat should launch the Java app and that Apache was supposed to launch your .bat. Sorry for this misunderstanding.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
execute shell command inside of c code? khucinx Programming 6 10-26-2010 10:30 PM
very new to C, how to execute command inside C? neurotic85 Programming 5 09-05-2005 09:19 AM
how to execute shell scripts from cgi script rajatgarg Linux - General 1 07-08-2004 02:39 PM
execute shell command inside of tcl file khucinx Programming 1 07-05-2004 02:29 PM
Failed to execute cgi script in Apache!! baby_linu Linux - Newbie 4 06-30-2004 05:23 PM


All times are GMT -5. The time now is 08:42 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration