用Wrapper做java后台服务器程序,java文件有要求么?

用Wrapper做java后台服务器程序,java文件有要求么?,第1张

分类: 电脑/网络 >>程序设计 >>其他编程语言

问题描述:

我想用Wrapper做java后台服务器程序,Wrapper配置完毕,但是运行App.bat时,提示如下错误:

wrapper | -->WrapperStarted as Console

wrapper | Launching a JVM...

jvm 1 | Wrapper(Version 3.2.1)wrapper.tanukisofarejvm 1 |

jvm 1 |

jvm 1 | WrapperSimpleApp:Encountered an error running main:javang.illegalAccessException:Class .tanukisofare.wrapper.WrapperSimpleApp can not access a member of class A with modifiers "public static"

jvm 1 | javang.illegalAccessException:Class .tanukisofare.wrapper.WrapperSimpleApp can not access a member of class A with modiffiers "public static"

jvm 1 | at sun.reflect.Reflection.ensureMemberAccess(Unknown Source)

jvm 1 | at javang.reflect.Method.invoke(Unkown Source)

jvm 1 | at .tanukisofare.wrapper.WrapperSimpleApp.run(WrapperSimpleApp.java:292)

jvm 1 | at javang.Thread.run(Unknown Source)

wrapper | 《--Wrapper Stopped

请按任意键继续...

请问这是怎么回事,如何解决?是否对类的写法又要求,是否有Main方法?

我的程序目录为:

|-bin-+

| |-wrapper.exe

| |-UninstallApp-NT.bat

| |-App.bat

| |-InstallApp_NT.bat

| |-A.class

|

|-conf-+

| |-wrapper.conf

|

|-logs-+

| |-wrapper.log

|

|-lib-+

|-wrapper.dll

|-wrapper.jar

以下是A.class的内容:

import javax.swing.*

import java.awt.*

import java.awt.event.*

import java.sql.*

/*引用短信类*/

class A extends JFrame{

private String url = "jdbc:mysql127.0.0.1:3306/test"

private String user = "root"

private String pass = ""

private String content,phones

private String[] tels

private int sdid

private Connection conn,delConn

private Statement sql

private ResultSet rs

A(){

super("短信发送系统")

/*短信注册代码 实例化短信类*/

this.addWindowListener(new WindowAdapter(){

public void windowClosing(WindowEvent e){

/*短信注销代码*/

System.exit(0)

}

})

}

public void runA(){

try{

Class.forName(".mysql.jdbc.Driver")

}catch(ClassNotFoundException e){

System.out.println(e)

}

try{

conn = DriverManager.getConnection(url,user,pass)

sql = conn.createStatement()

rs = sql.executeQuery("SELECT * FROM ok")

while(rs.next()){

sdid = rs.getInt(1)

content = rs.getString(3)

phones = rs.getString(6)/*发送短信代码*/

}

}

rs.close()

sql.close()

conn.close()

}catch(SQLException sqle){

System.out.println(sqle)

}

}

public static void main(String[] args){

A a = new A()

a.runA()

}

}

请高手帮忙指点谢谢!

解析:

新建一个类,把A类主方法写到这个类中.

在Windows中,无法直接用Java产生Service程序;但是可能通过编写DCOM 或 MTS COM 或 COM+(Window2000) 组件 或 Corba 来模拟Service程序,可以实现透明远程过程调用;

StdAfx.h

#if !defined(AFX_STDAFX_H__7CCCEEAD_83A0_11D4_B105_000021E19FBF__INCLUDED_)

#define AFX_STDAFX_H__7CCCEEAD_83A0_11D4_B105_000021E19FBF__INCLUDED_

#if _MSC_VER >1000

#pragma once

#endif // _MSC_VER >1000

#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers

#include <stdio.h>

#include <stdlib.h>

#include <windows.h>

#include <winsvc.h>

#endif

//----------------------------------------------------------------

service.h

// Service.h: interface for the CService class.

//

//////////////////////////////////////////////////////////////////////

#if !defined(AFX_SERVICE_H__7CCCEEB1_83A0_11D4_B105_000021E19FBF__INCLUDED_)

#define AFX_SERVICE_H__7CCCEEB1_83A0_11D4_B105_000021E19FBF__INCLUDED_

#if _MSC_VER >1000

#pragma once

#endif // _MSC_VER >1000

typedef struct T_SERVICE

{

char ServiceName[128]

char DisplayName[128]

char ExecutePath[_MAX_PATH]

}T_SERVICE

BOOL LoadServiceSetting(const char* ServiceName,T_SERVICE* pService)

BOOL SaveServiceSetting(T_SERVICE* pService)

void DeleteServiceSetting(const char* ServiceName)

int outputerr(DWORD nErr=0)

inline BOOL outputerr(long nErr)

{

outputerr((DWORD)nErr)

return FALSE

}

#ifdef _DEBUGLOG

#define WRITELOG writelog

void writelog(const char* fmt,...)

#else

#define WRITELOG

#endif

VOID WINAPI ServiceMain(DWORD dwArgc, LPTSTR *lpszArgv)

#endif // !defined(AFX_SERVICE_H__7CCCEEB1_83A0_11D4_B105_000021E19FBF__INCLUDED_)

//----------------------------------------------------------------

StdAfx.cpp

#include "stdafx.h"

//----------------------------------------------------------------

Service.cpp

// Service.cpp: implementation of the CService class.

//

//////////////////////////////////////////////////////////////////////

#include "stdafx.h"

#include "Service.h"

const char REG_PATH[] = "Software\\Free\\ServiceAny"

const char REG_DISPLAYNAME[] = "DisplayName"

const char REG_SERVICENAME[] = "ServiceName"

const char REG_EXECUTEPATH[] = "ExecutePath"

void DeleteServiceSetting(const char* ServiceName)

{

char temp[1024]

sprintf(temp,"%s\\%s",REG_PATH,ServiceName)

::RegDeleteKey(HKEY_LOCAL_MACHINE,temp)

}

BOOL LoadServiceSetting(const char* ServiceName,T_SERVICE* pService)

{

char temp[1024]

sprintf(temp,"%s\\%s",REG_PATH,ServiceName)

HKEY hKey=NULL

long r = ::RegOpenKeyEx(

HKEY_LOCAL_MACHINE,

temp,NULL,KEY_READ,&hKey)

if (r != 0)

return outputerr(r)

DWORD Type=0

DWORD cbtemp = sizeof(temp)

r = ::RegQueryValueEx(hKey,REG_SERVICENAME,NULL,&Type,(LPBYTE)temp,&cbtemp)

if (r != 0 || Type != REG_SZ)

{

::RegCloseKey(hKey)

return outputerr(r)

}

strcpy(pService->ServiceName,temp)

cbtemp = sizeof(temp)

r = ::RegQueryValueEx(hKey,REG_DISPLAYNAME,NULL,&Type,(LPBYTE)temp,&cbtemp)

if (r == 0 &&Type == REG_SZ)

strcpy(pService->DisplayName,temp)

else

strcpy(pService->DisplayName,pService->ServiceName)

cbtemp = sizeof(temp)

r = ::RegQueryValueEx(hKey,REG_EXECUTEPATH,NULL,&Type,(LPBYTE)temp,&cbtemp)

if (r != 0 || Type != REG_SZ)

{

::RegCloseKey(hKey)

return outputerr(r)

}

strcpy(pService->ExecutePath,temp)

::RegCloseKey(hKey)

return TRUE

}

static LONG SetRegValue(HKEY hKey,const char* Name,const char* Value)

{

return ::RegSetValueEx(hKey,Name,NULL,REG_SZ,(CONST BYTE*)Value,strlen(Value)+1)

}

BOOL SaveServiceSetting(T_SERVICE* pService)

{

char temp[1024]

sprintf(temp,"%s\\%s",REG_PATH,pService->ServiceName)

HKEY hKey=NULL

DWORD cbtemp=0

long r = ::RegCreateKeyEx(

HKEY_LOCAL_MACHINE,temp,NULL,

"",NULL,KEY_WRITE,NULL,&hKey,&cbtemp)

if (r != 0)

return outputerr(r)

if (strlen(pService->DisplayName)==0)

{

strcpy(pService->DisplayName,pService->ServiceName)

}

r = SetRegValue(hKey,REG_SERVICENAME,pService->ServiceName)

if (r != 0)

{

::RegCloseKey(hKey)

return outputerr(r)

}

r = SetRegValue(hKey,REG_DISPLAYNAME,pService->DisplayName)

if (r != 0)

{

::RegCloseKey(hKey)

return outputerr(r)

}

r = SetRegValue(hKey,REG_EXECUTEPATH,pService->ExecutePath)

if (r != 0)

{

::RegCloseKey(hKey)

return outputerr(r)

}

::RegCloseKey(hKey)

return TRUE

}

#ifdef _DEBUGLOG

void writelog(const char* fmt,...)

{

charmsg[2048]

va_list arg

va_start(arg, fmt)

vsprintf(msg, fmt, arg)

va_end(arg)

FILE* f = fopen("f:\\SrvAny.log","at")

fprintf(f,"%s",msg)

fclose(f)

}

#endif

static char ServiceName[128]

BOOL bLogError = FALSE

void LogEvent(const char* fmt,...)

{

charmsg[2048]

HANDLE hEventSource

LPTSTR lpszStrings[1]

va_list arg

va_start(arg, fmt)

vsprintf(msg, fmt, arg)

va_end(arg)

lpszStrings[0] = msg

hEventSource = RegisterEventSource(NULL, ServiceName)

if (hEventSource != NULL)

{

/* Write to event log. */

ReportEvent(hEventSource,

bLogError?EVENTLOG_ERROR_TYPE:EVENTLOG_INFORMATION_TYPE,

0, 0, NULL, 1, 0, (LPCTSTR*) &lpszStrings[0], NULL)

DeregisterEventSource(hEventSource)

}

printf("%s",msg)

}

static SERVICE_STATUS_HANDLE hServiceStatus=NULL

static SERVICE_STATUS ServiceStatus

static T_SERVICE Service

static HANDLE hShutdownEvent=NULL

static HANDLE hThread=NULL

DWORD MainThreadId=0

void SetServiceStatus(DWORD status)

{

ServiceStatus.dwCurrentState = status

::SetServiceStatus(hServiceStatus,&ServiceStatus)

}

void LogError(DWORD nErr=0)

{

if (nErr==0)

nErr = ::GetLastError()

char temp[1024]

DWORD dwLen = ::FormatMessage(

FORMAT_MESSAGE_FROM_SYSTEM |

FORMAT_MESSAGE_IGNORE_INSERTS,NULL,

nErr,NULL,temp,sizeof(temp),NULL)

if (dwLen >0)

temp[dwLen] = NULL

else

temp[0] = NULL

bLogError = TRUE

LogEvent("Win32 Error(%d) %s\n",nErr,temp)

ServiceStatus.dwWin32ExitCode = ERROR_SERVICE_SPECIFIC_ERROR

ServiceStatus.dwServiceSpecificExitCode = nErr

}

static void WINAPI ServiceHandler(DWORD dwOpcode)

{

switch (dwOpcode)

{

case SERVICE_CONTROL_STOP:

case SERVICE_CONTROL_SHUTDOWN:

SetServiceStatus(SERVICE_STOP_PENDING)

PostThreadMessage(MainThreadId, WM_QUIT, 0, 0)

break

case SERVICE_CONTROL_PAUSE:

case SERVICE_CONTROL_CONTINUE:

case SERVICE_CONTROL_INTERROGATE:

default:

break

}

}

static DWORD WINAPI ThreadProc(LPVOID lpParameter)

{

PROCESS_INFORMATION pi

STARTUPINFO si

ZeroMemory(&si,sizeof(si))

si.cb = sizeof(si)

char CommandLine[2048]

strcpy(CommandLine,Service.ExecutePath)

if (lpParameter != NULL &&strlen((const char*)lpParameter)>0)

{

strcat(CommandLine," ")

strcat(CommandLine,(const char*)lpParameter)

}

char exedir[_MAX_PATH]

strcpy(exedir,Service.ExecutePath)

int n = strlen(exedir)

for(int i=n-1i>=0i--)

{

char c = exedir[i]

exedir[i] = NULL

if (c == '\\')

break

}

if (!::CreateProcess(

Service.ExecutePath,CommandLine,

NULL,NULL,TRUE,

NULL,NULL,exedir,&si,&pi))

{

LogError()

LogEvent("can't CreateProcess %s\n",CommandLine)

::PostThreadMessage(MainThreadId, WM_QUIT, 0, 0)

return (DWORD)-1

}

SetServiceStatus(SERVICE_RUNNING)

HANDLE events[2]

events[0] = pi.hProcess

events[1] = hShutdownEvent

DWORD r = ::WaitForMultipleObjects(2,events,FALSE,INFINITE)

if (r != WAIT_OBJECT_0)

{

::PostThreadMessage(pi.dwThreadId,WM_QUIT,0,0)

DWORD r1 = ::WaitForSingleObject(pi.hProcess,10000)

if (r1 == WAIT_TIMEOUT)

{

LogEvent("Can't stop %s normally.\n",Service.ExecutePath)

::TerminateProcess(pi.hProcess,-1)

}

}

if (r != WAIT_OBJECT_0 + 1)

{

::PostThreadMessage(MainThreadId, WM_QUIT, 0, 0)

}

DWORD ec=-1

::GetExitCodeProcess(pi.hProcess,&ec)

::CloseHandle(pi.hProcess)

::CloseHandle(pi.hThread)

return ec

}

VOID WINAPI ServiceMain(DWORD dwArgc, LPTSTR *lpszArgv)

{

WRITELOG("in ServiceMain,argc=%d\n",dwArgc)

for(DWORD ii=0ii<dwArgcii++)

{

WRITELOG("argv[%d] = %s\n",ii,lpszArgv[ii])

}

WRITELOG("\n")

strcpy(ServiceName, lpszArgv[0])

char param[2048]

param[0] = NULL

for (DWORD i=1i<dwArgci++)

{

if (i >1)

strcat(param," ")

if (strchr(lpszArgv[i],' ') == NULL)

strcat(param,lpszArgv[i])

else

{

strcat(param,"\"")

strcat(param,lpszArgv[i])

strcat(param,"\"")

}

}

MainThreadId = ::GetCurrentThreadId()

ZeroMemory(&ServiceStatus,sizeof(ServiceStatus))

ServiceStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS

ServiceStatus.dwCurrentState = SERVICE_STOPPED

ServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP|SERVICE_ACCEPT_SHUTDOWN

hServiceStatus = RegisterServiceCtrlHandler(ServiceName, ServiceHandler)

SetServiceStatus(SERVICE_START_PENDING)

if (!LoadServiceSetting(ServiceName,&Service))

LogEvent("Can't find Service Setting for '%s'\n",ServiceName)

else

{

hShutdownEvent = ::CreateEvent(NULL,TRUE,FALSE,NULL)

DWORD ThreadId=0

hThread = ::CreateThread(NULL,NULL,ThreadProc,param,NULL,&ThreadId)

MSG msg

while (GetMessage(&msg, 0, 0, 0))

DispatchMessage(&msg)

SetEvent(hShutdownEvent)

WaitForSingleObject(hThread,INFINITE)

::CloseHandle(hThread)

::CloseHandle(hShutdownEvent)

}

SetServiceStatus(SERVICE_STOPPED)

}

//----------------------------------------------------------------

SrvAny.cpp

// SrvAny.cpp : Defines the entry point for the console application.

//

#include "stdafx.h"

#include <crtdbg.h>

#include "Service.h"

int RegisterService(const char* ServiceName,const char* DisplayName,const char* ExecutePath)

int UnregisterService(const char* ServiceName)

int outputerr(DWORD nErr)

{

if (nErr==0)

nErr = ::GetLastError()

_ASSERT(nErr != 0)

char temp[1024]

DWORD dwLen = ::FormatMessage(

FORMAT_MESSAGE_FROM_SYSTEM |

FORMAT_MESSAGE_IGNORE_INSERTS,NULL,

nErr,NULL,temp,sizeof(temp),NULL)

printf("Win32 Error(%d)",nErr)

if (dwLen >0)

printf("\t%s",temp)

printf("\n")

return -1

}

int usage()

{

printf("SrvAny usage\n")

printf("\t/s ServiceName [DisplayName] ExecutePath\tRegister Service\n")

printf("\t/u ServiceName \tUnregister Service\n")

printf("\n")

return -1

}

int main(int argc, char* argv[])

{

WRITELOG("in main,argc=%d\n",argc)

for(int i=0i<argci++)

{

WRITELOG("argv[%d] = %s\n",i,argv[i])

}

WRITELOG("\n")

if (argc == 1)

{

SERVICE_TABLE_ENTRY st[] =

{

{ "", ServiceMain },

{ NULL, NULL }

}

if (!::StartServiceCtrlDispatcher(st))

return usage()

else

return 0

}

if (strcmp(argv[1],"/?")==0)

return usage()

if (strcmp(argv[1],"/s")==0)

{

if (argc == 4)

return RegisterService(argv[2],argv[2],argv[3])

if (argc == 5)

return RegisterService(argv[2],argv[3],argv[4])

return usage()

}

if (strcmp(argv[1],"/u")==0)

{

if (argc != 3)

return usage()

else


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/7828495.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-10
下一篇 2023-04-10

发表评论

登录后才能评论

评论列表(0条)

保存