Advertisement
Guest User

Untitled

a guest
Sep 18th, 2019
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.56 KB | None | 0 0
  1. function Java-CompileRun {
  2. Param(
  3. [Parameter(Mandatory=$true)]
  4. [String]
  5. $javaFile,
  6. [Parameter(Mandatory=$false)]
  7. [String]
  8. $path=$PWD,
  9. [Parameter(Mandatory=$false)]
  10. [String]
  11. $jdkPath=$($(where.exe javac) -replace '\\[^\\]+$','')
  12. )
  13. $path = $path -replace '\\$',''
  14. $jdkPath = $jdkPath -replace '\\$',''
  15. if (-not (Test-Path -Path "$path\$javaFile") -and (Test-Path -Path "$path\$javaFile.java")) {
  16. $javaFile = "$javaFile.java"
  17. }
  18. $quit = $false;
  19. if(-not (Test-Path -Path "$jdkPath\javac.exe")){Write-Error -Message "Cannot find javac.exe at $jdkPath." -Category ObjectNotFound -RecommendedAction "Please ensure you are providing the correct path to the JDK bin directory"; $quit = $true;}
  20. if(-not (Test-Path -Path "$jdkPath\java.exe")){Write-Error -Message "Cannot find java.exe at $jdkPath." -Category ObjectNotFound -RecommendedAction "Please ensure you are providing the correct path to the JDK bin directory"; $quit = $true;}
  21. if(-not (Test-Path -Path "$path\$javaFile")) {Write-Error -Message "Cannot find $javaFile at $path\$javaFile." -Category ObjectNotFound -RecommendedAction "Please ensure you are providing the correct path and the correct filename to the .java file"; $quit = $true; }
  22. if($quit){return}
  23.  
  24. $_javac = Start-Process -FilePath "$($jdkPath)\javac.exe" -ArgumentList "-Xlint:all $javaFile" -WorkingDirectory $Path -NoNewWindow -PassThru -Wait
  25. if($_javac.ExitCode -eq 0){
  26. $_java = Start-Process -FilePath "$($jdkPath)\java.exe" -ArgumentList "$($javaFile -replace '.java','')" -WorkingDirectory $path -NoNewWindow -PassThru -Wait
  27. }
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement