게시판 즐겨찾기
편집
드래그 앤 드롭으로
즐겨찾기 아이콘 위치 수정이 가능합니다.
어제 올렸던 웹 컴파일러 구현 미약하게나마 해결되었습니다.
게시물ID : programmer_21197짧은주소 복사하기
작성자 : 하린
추천 : 0
조회수 : 513회
댓글수 : 1개
등록시간 : 2017/08/08 20:36:38
이전글 : 
http://www.todayhumor.co.kr/board/view.php?table=programmer&no=21188&s_no=13701098&kind=member&page=1&member_kind=total&mn=349143
-----------------------------------------------------------------------------------------------------------


파일이 하나일때는 컴파일이 잘 되고 2개 일때는 잘 안되었죠.
spring 환경이냐, standard 환경이냐에 따라 안되는건 줄 알았는데
단순, classPath를 잘못 설정해줘서 그랬던거네요. 

//classPath : 클래스 패스 경로
//examFiles : java 파일 저장할 경로 

public Object examSampleCompile(
String filePath, String currProblemNum,
@RequestParam(value="dataSourceArr[]") String[] dataSourceArr,
@RequestParam(value="dataNameArr[]") String[] dataNameArr) throws Exception{

//examFiles를 전역변수로 만들었기 때문에 examFiles의 경로가 완성되면 filePath의 경로를 더하지 않는다.
if(examFiles.indexOf(filePath) < 0){
examFiles += filePath; 
}
File[] files = javaCreateFile(dataSourceArr, dataNameArr);

String name = dataNameArr[0];

for(int i=0; i<files.length; i++){
compile(files[i].getPath(), classPath, name);
}
//구글링 했을때 대부분이 newInstance로 인스턴스를 생성하길래 당연히 해줘야 되는 줄 알았는데,
//저의 경우, main() 함수만 호출한 결과를 가져오는거라 해당 클래스 찾고 main 함수 찾아서 실행시키만 하면 되었었네요 -,.-;
String classForName = examFiles + dataNameArr[0];
Class cls = Class.forName(classForName.replace("/", "."));
Method m = cls.getMethod("main",String[].class);       
        m.invoke(null, new Object[]{null}); 
        
        return null;
};
----------------------------------------------------------------------------------------------------------
//codemirror에 있는 내용을 .java 파일로 만드는 메서드 입니다.
public File[] javaCreateFile(String[] dataSourceArr, String[] dataNameArr) throws Exception{
File[] files = new File[dataSourceArr.length];

// 디렉토리 생성
File saveFolder = new File(classPath + examFiles) ;
if(!saveFolder.exists()){
saveFolder.mkdirs();
}
for(int i=0; i<dataNameArr.length; i++){ 
if(dataNameArr[i].length() != 0){
String saveName = dataNameArr[i] + ".java";
//파일 생성
File file = new File(saveFolder, saveName);
file.createNewFile();
FileWriter fw = new FileWriter(file);
fw.write(dataSourceArr[i]);
fw.flush();
fw.close();
files[i] = file;
}
}
return files;
}
-----------------------------------------------------------------------------------------------------------
//java파일을 class 파일로 컴파일 해주는 메서드 입니다.
      //stackOverFlow에서 참고 하였는데 검색할 때 키워드가 java compiler class... 였나... 검색을 너무 해대서 정확하게 기억이 나질 않는군요.
      //북마크를 해두었었야 했는데... 무튼 거기서 참고한 소스인데 거기서는 -classpath가 아닌 -d 였나?? 그걸 사용했더라구요.
      //아마도 cmd에서 java 컴파일 할때 사용하는 명령어같은데 ... 
public void compile(String sourceFilePath, String classPath, String name) throws Exception {
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
Iterable sourcefiles = fileManager.getJavaFileObjects(sourceFilePath);
Iterable<String> options = Arrays.asList("-classpath", classPath);
compiler.getTask(null, fileManager, null, options, null, sourcefiles).call();
fileManager.close();
}
전체 추천리스트 보기
새로운 댓글이 없습니다.
새로운 댓글 확인하기
글쓰기
◀뒤로가기
PC버전
맨위로▲
공지 운영 자료창고 청소년보호