Spawning Java from C
James Rome
jamesrome@gmail.com
Thu Feb 23 17:35:00 GMT 2012
I have code that launches Java from C to run a jar file, and creates a
socket to communicate between the calling program and the jar file. It
works in OS X, Linux, and MinGW on Windows, but not on Cygwin.
My first question is that when I run
$ cygcheck -s -v -r > cygcheck.out
/usr/bin/cygrunsrv: warning: OpenService failed for 'DcomLaunch': Win32
error 5
Access is denied.
/usr/bin/cygrunsrv: warning: OpenService failed for 'ose': Win32 error 5
Access is denied.
/usr/bin/cygrunsrv: warning: OpenService failed for 'ose64': Win32 error 5
Access is denied.
/usr/bin/cygrunsrv: warning: OpenService failed for 'osppsvc': Win32 error 5
Access is denied.
/usr/bin/cygrunsrv: warning: OpenService failed for 'pla': Win32 error 5
Access is denied.
/usr/bin/cygrunsrv: warning: OpenService failed for 'QWAVE': Win32 error 5
Access is denied.
/usr/bin/cygrunsrv: warning: OpenService failed for 'RpcEptMapper':
Win32 error 5
Access is denied.
/usr/bin/cygrunsrv: warning: OpenService failed for 'RpcSs': Win32 error 5
Access is denied.
/usr/bin/cygrunsrv: warning: OpenService failed for
'WPFFontCache_v0400': Win32 error 5
Access is denied.
Some things do not work, so is Cygwin installed properly?
-----------------------------------
What happens when I run the code is that the spawned Java cannot find
the jar file, even with its full path ( headed by either /cygdrive/c/...
and \\C:\\....).
$ ./tkfpush.exe 4560 Bar.tkf t
Client: WSAStartup() is OK.
jar location="/cygdrive/c/Users/jar/Tekdraw2/tekPlot/dist/tekPlot.jar"
Client: WSAStartup() is OK.
Client: socket() is OK.
Unable to access jarfile
"/cygdrive/c/Users/jar/Tekdraw2/tekPlot/dist/tekPlot.jar"
socket connect failed: No error
Client: connect() - Failed to connect.
The file is there:
jar@jari7 ~
$ ls /cygdrive/c/Users/jar/Tekdraw2/tekPlot/dist/
README.TXT lib tekPlot.jar
------------------------
The relevant code is:
#define TP_LOCATION =
"/cygdrive/c/Users/jar/Tekdraw2/tekPlot/dist/tekPlot.jar"
#define JAVA_LOCATION ="/cygdrive/c/Windows/system32/java"
#define MIN_PORT 49158
#define MAX_PORT 65535
#define BUFFLEN 4096
#define GPC_CYGWIN
#ifdef GPC_CYGWIN
// this stops the FD definitions in types.h
#define __USE_W32_SOCKETS
#endif
#include <stdio.h> // includes sys/types.h for cygwin
#include <errno.h>
#include "GPCdefs.h" // must be before sys/types
//#include <sys/types.h>
#define __USE_W32_SOCKETS
#include <dirent.h> // This includes sys/types.h
#ifdef GPC_WIN
#define __USE_W32_SOCKETS
#include <windows.h>
#include <process.h>
#include <time.h>
#ifdef GPC_CYGWIN
//#include <w32api/winsock2.h> // included by windows.h
#include <w32api/winbase.h>
#include <string.h>
#endif
#ifdef GPC_MINGW
#include <winsock2.h>
#include <winbase.h>
#include <string.h>
#endif
#endif
int
main(int argc, char** argv) {
int i;
char portstr[10];
int port;
char *my_env[5];
char * filename;
int launch_java = 1;
char *tp_loc TP_LOCATION;
char *java_loc JAVA_LOCATION;
if (argc <= 3) {
printf("Usage: tkfPush port tkf_file_name_path launch_java(t/f)");
} else {
strncpy(portstr, argv[1], 10);
if (argc > 3) {
launch_java = *argv[3] == 't' ? 1 : 0;
}
port = atoi(argv[1]);
filename = argv[2];
// Get the environment
my_env[0] = getenv("PATH");
port = 5556;
if (port == 0) { // Failure
fprintf(stderr, "Failed to find an open port in C");
exit(-1);
}
sprintf(portstr, "%d", port);
fprintf(stdout, "jar location=%s\n", tp_loc);
// start Java
if (spawnlp(_P_NOWAIT, "java",
java_loc,
"-jar", tp_loc,
portstr, NULL) < 0) {
perror("spawnlp failed");
exit(EXIT_FAILURE);
}
// Initialize Winsock.
WSADATA wsaData;
int iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (iResult != NO_ERROR)
printf("Client: Error at WSAStartup().\n");
else
printf("Client: WSAStartup() is OK.\n");
// Create a socket.
SOCKET m_socket;
m_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (m_socket == INVALID_SOCKET) {
printf("Client: socket() - Error at socket(): %ld\n",
WSAGetLastError());
WSACleanup();
return 0;
} else
printf("Client: socket() is OK.\n");
// Connect to a server.
struct sockaddr_in clientService;
clientService.sin_family = AF_INET;
clientService.sin_addr.s_addr = inet_addr("127.0.0.1");
clientService.sin_port = htons(port);
/* Must loop to give the Java process time to start */
time_t now = time(NULL);
time_t test;
for (i = 0; i < 10; i++) {
while (time(NULL) < now + 2l); // wait a bit
if (connect(m_socket,
(SOCKADDR*) & clientService,
sizeof (clientService))
!= SOCKET_ERROR)
break; /* Successful */
now = time(NULL);
if (i == 9) {
perror("socket connect failed");
printf("Client: connect() - Failed to connect.\n");
WSACleanup();
return 0;
}
}
// Send and receive data.
int bytesSent;
/* Now send the file name over the pipe */
bytesSent = send(m_socket, "#f", 2, 0);
bytesSent = send(m_socket, filename, strlen(filename), 0);
printf("Client: send() - Bytes Sent: %ld\n", bytesSent);
bytesSent = send(m_socket, "#", 1, 0);
} // End else
return (EXIT_SUCCESS);
}
So, what am I doing wrong?
Thanks for the help,
Jim
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: cygcheck.out
URL: <http://cygwin.com/pipermail/cygwin/attachments/20120223/aa6fe41e/attachment.ksh>
-------------- next part --------------
--
Problem reports: http://cygwin.com/problems.html
FAQ: http://cygwin.com/faq/
Documentation: http://cygwin.com/docs.html
Unsubscribe info: http://cygwin.com/ml/#unsubscribe-simple
More information about the Cygwin
mailing list