#include
#include
#include
void ToRoot(char* dir) //根据路径获取根目录
{
for(unsigned i=0;i
if(dir[i]=='\\')
{
dir[i]=0;
return;
}
}
void SetClassPathEnv(char *buf , unsigned size) //设置ClassPath环境变量
{
memset(buf,0,200);
strcpy(buf,\"CLASSPATH=.;\");
strcat(buf,getenv(\"java_home\"));
strcat(buf,\"\\bin;\");
strcat(buf,getenv(\"java_home\"));
strcat(buf,\"\\jre\\lib;\");
putenv(buf);
}
void SetPathEnv(char *buf,unsigned size) //设置Path环境变量
{
memset(buf,0,size);
strcpy(buf,\"PATH=\");
strcat(buf,getenv(\"java_home\"));
strcat(buf,\"\\bin;\");
strcat(buf,getenv(\"java_home\"));
strcat(buf,\"\\lib;\");
strcat(buf,getenv(\"java_home\"));
strcat(buf,\"\\jre\\lib;\");
strcat(buf,getenv(\"PATH\"));
putenv(buf);
}
void SetJavaEnv() //设置java所需环境变量
{
const unsigned max=500;
char dir[50];
GetCurrentDirectory(50,dir);
ToRoot(dir);
strcat(dir,\"\\Software\\JDK\"); //因为我的jdk目录是放在我的移动硬盘的X:\SoftWare下的
char buf[max]={0};
strcpy(buf,\"JAVA_HOME=\");
strcat(buf,dir);
putenv(buf);
SetClassPathEnv(buf,max);
SetPathEnv(buf,max);
}
//------------------------------------------------------
//两个main用来编写两个程序,都用到上面SetJavaPath()的方法
//------------------------------------------------------
//javac编译
int main(unsigned argn,char *args[])
{
if(argn==1)
return -1;
SetJavaEnv();
char cmd[100]={0};
strcpy(cmd,\"javac \");
strcat(cmd,args[1]);
system(cmd);
return 0;
}
//====================================================
//java运行
int main(unsigned argn,char *args[])
{
if(argn==1)
return -1;
SetJavaEnv();
char cmd[100]={0};
strcpy(cmd,\"java \");
for(unsigned i=1;i
{
strcat(cmd,args[i]);
strcat(cmd,\" \");
}
system(cmd);
return 0;
}