买了移动硬盘总希望学习工具都在上面,方便随时使用,但又因为java老要
设置环境变量,所以写了两个程序代替javac.exe和java.exe.
当然,说是代替,其实就是在调用javac,java前设置临时环境变量而已,
不过倒是方便了我
#include <stdlib.h>
#include <Windows.h>
#include <Winbase.h>
void ToRoot(char* dir) //根据路径获取根目录
{
for(unsigned i=0;i<strlen(dir);i++)
if(dir=='\\')
{
dir=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=