Reputation: 13
I am trying to connect to SSH (Unix environment) using the JSch library in JavaCompiler API. I can connect to the unix environment through a normal Java program but cannot from JavaCompiler API.
Looks like it is related to crypto.policy Cipher length error but I am not sure how to solve it in JavaCompiler API.
Stuck for 2 Days....
Complete Error:
Exception in thread "main" java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at dummy.dummy_Main.main(dummy_Main.java:324)
Caused by: java.lang.ExceptionInInitializerError
at javax.crypto.Cipher.getInstance(Cipher.java:518)
at com.jcraft.jsch.jce.AES256CTR.init(AES256CTR.java:56)
at com.jcraft.jsch.Session.checkCipher(Session.java:2497)
at com.jcraft.jsch.Session.checkCiphers(Session.java:2474)
at com.jcraft.jsch.Session.send_kexinit(Session.java:624)
at com.jcraft.jsch.Session.connect(Session.java:307)
at com.jcraft.jsch.Session.connect(Session.java:183)
at test.Test.bpmUnlock(Test.java:180)
at test.Test.start(Test.java:221)
... 5 more
Caused by: java.lang.SecurityException: Can not initialize cryptographic mechanism
at javax.crypto.JceSecurity.<clinit>(JceSecurity.java:93)
... 14 more
Caused by: java.lang.SecurityException: Cannot locate policy or framework files!
at javax.crypto.JceSecurity.setupJurisdictionPolicies(JceSecurity.java:316)
at javax.crypto.JceSecurity.access$000(JceSecurity.java:50)
at javax.crypto.JceSecurity$1.run(JceSecurity.java:85)
at java.security.AccessController.doPrivileged(Native Method)
at javax.crypto.JceSecurity.<clinit>(JceSecurity.java:82)
... 14 more
Sample code:(Ignore if any compile error)
package dummy;
import java.io.*;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.security.Security;
import java.util.Vector;
import javax.tools.JavaCompiler;
import javax.tools.ToolProvider;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
public class dummy_Main{
//supportive variables
public static void main(String args[]) throws Exception {
//String source = masterVector.get(0).get(15);
String source = "package test;\r\n"
+ "//jsch library\r\n"
+ "import com.jcraft.jsch.JSch;\r\n"
+ "import com.jcraft.jsch.Session;\r\n"
+ "import com.jcraft.jsch.Channel;\r\n"
+ "import com.jcraft.jsch.ChannelExec;\r\n"
+ "\r\n"
+ "//Security\r\n"
+ "import java.security.Security;\r\n"
+ "\r\n"
+ "\r\n"
+ "public class Test{\r\n"
+ "\r\n"
+ " public List<Object> bpmUnlock(String incident, Vector<String> RPA_SEARCH_CRITERIA,Vector<String> INCIDENT_INFO) throws Exception{\r\n"
+ "\r\n"
+ " try {\r\n"
+ " System.out.println(\"JSCH Not loaded.\");\r\n"
+ " JSch connect = new JSch();\r\n"
+ " System.out.println(\"JSCH loaded.\");\r\n"
+ " Session session = connect.getSession(user,host,port);\r\n"
+ " session.setPassword(pass);\r\n"
+ " System.out.println(\"password set\");\r\n"
+ " java.util.Properties config = new java.util.Properties(); \r\n"
+ " config.put(\"StrictHostKeyChecking\", \"no\");\r\n"
+ " session.setConfig(config);\r\n"
+ " System.out.println(\"COnfig Set\");\r\n"
+ " session.connect();\r\n"
+ " System.out.println(\"session connect\");\r\n"
+ " Channel channel=session.openChannel(\"exec\");\r\n"
+ " \r\n"
+ " System.out.println(\"channel opened\");\r\n"
+ " ((ChannelExec)channel).setCommand(\"echo \\\"Hello\\\"\");\r\n"
+ " Thread.sleep(5000);\r\n"
+ " channel.connect();\r\n"
+ " System.out.println(\"channel connected.\");\r\n"
+ " \r\n"
+ " channel.disconnect();\r\n"
+ " session.disconnect();\r\n"
+ " File f = new File(\"User_unlock_script_executed.txt\");\r\n"
+ " f.createNewFile();\r\n"
+ " FileWriter w = new FileWriter(f);\r\n"
+ " w.close();\r\n"
+ " }\r\n"
+ " catch (Exception e) {\r\n"
+ " // TODO: handle exception\r\n"
+ " System.out.println(e.toString());\r\n"
+ " }\r\n"
+ " }\r\n"
+ " }\r\n"
+ "\r\n"
+ "\r\n"
+ " //RPA_SEARCH_CRITERIA.set(16, resolution);\r\n"
+ "\r\n"
+ " //0 : For movement keep 0\r\n"
+ " //1 : For resolve keep 1\r\n"
+ "\r\n"
+ " infoObjects = Arrays.asList(incident,RPA_SEARCH_CRITERIA,INCIDENT_INFO,0);\r\n"
+ "\r\n"
+ " return infoObjects;\r\n"
+ "\r\n"
+ " } catch (Exception e) {\r\n"
+ " System.out.println(\"In BPM UNLOCK FUNCTION :\"+e);\r\n"
+ " return infoObjects;\r\n"
+ " }\r\n"
+ " }\r\n"
+ "\r\n"
+ " public List<Object> start(String incident, Vector<String> RPA_SEARCH_CRITERIA,Vector<String> INCIDENT_INFO) throws Exception{\r\n"
+ " return bpmUnlock(incident,RPA_SEARCH_CRITERIA,INCIDENT_INFO);\r\n"
+ " }\r\n"
+ "}";
//try {
File root = new File("/java");
File sourceFile = new File(root,"test/Test.java");
sourceFile.getParentFile().mkdirs();
Files.write(sourceFile.toPath(), source.getBytes(StandardCharsets.UTF_8));
JSONParser jsonParser = new JSONParser();
Object obj = jsonParser.parse(new FileReader("config.json"));
JSONObject jsonObject = (JSONObject)obj;
String name = (String)jsonObject.get("JDK_PATH");
System.out.println(name);
System.setProperty("java.home", name);
//System.setProperty("java.home", "C:\\Program Files\\Java\\jdk-18.0.2");
System.out.println(System.getProperty("java.home"));
System.out.println(System.getProperty("java.version"));
System.out.println(System.getProperty("java.vm.version"));
Security.setProperty("crypto.policy", "unlimited");
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
System.out.println(ToolProvider.getSystemJavaCompiler());
System.out.println(sourceFile.getPath());
int flag = compiler.run(null, null, null, sourceFile.getPath());
System.out.println(flag);
if(flag == 0) {
System.out.println("Compilation successful.");
URLClassLoader classLoader = URLClassLoader.newInstance(new URL[] {root.toURI().toURL()});
Class<?> cls = Class.forName("test.Test",true,classLoader);
Object instance = cls.getDeclaredConstructor().newInstance();
Method startMethod = instance.getClass().getMethod("start",String.class,Vector.class,Vector.class);
Vector<String> v = new Vector<>();
Vector<String> l = new Vector<>();
v.add("sarage");
l.add("sarage");
startMethod.invoke(instance,"",v,l);
//System.out.println(startMethod.invoke(instance,"",v,l));
}
// }
// catch (Exception e) {
// System.out.println("In JavaCompiler Error:\n"+e);
// }
}
}
I tried setting : Security.setProperty("crypto.policy", "unlimited");
I tried adding crypto.policy=unlimited
in java.security
file
Also tried adding local_policy.jar
and US_export_policy C:\\Program Files (x86)\\Java\\jre1.8.0_301\\lib\\security
folder
Upvotes: 1
Views: 503