Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

java.lang.ClassNotFoundException: com.sun.tools.javac.api.JavacTool while executing jar in command line #52

Closed
umeshshende opened this issue Sep 27, 2020 · 12 comments

Comments

@umeshshende
Copy link

umeshshende commented Sep 27, 2020

I have an app that taking name(excel sheet) from user and I use those name as method and compile at runtime it's working in eclipse when running the project (not from command line ) but getting java.lang.ClassNotFoundException: com.sun.tools.javac.api.JavacTool exception when running batch file containing command for executing jar file(which has logic for runtime compilation ) I have try with the added dependency in maven com.sun artifact mentioned in the issue but did not work any suggestions

@umeshshende umeshshende changed the title java.lang.ClassNotFoundException: com.sun.tools.javac.api.JavacTool while execute jar in command line java.lang.ClassNotFoundException: com.sun.tools.javac.api.JavacTool while executing jar in command line Sep 27, 2020
@dpisklov
Copy link
Contributor

Duplicate of #46

@dpisklov dpisklov marked this as a duplicate of #46 Sep 27, 2020
@umeshshende
Copy link
Author

umeshshende commented Sep 28, 2020

#46 i have gone through the solution but JDK already installed in my local machine for executing jar file local machine(Window 10) using batch command
but still getting
Error
Reading main index file.....
Envoirement value of folderName :20200928_135704
Reading main index file finish
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Exception in thread "main" java.lang.AssertionError: java.lang.ClassNotFoundException: com.sun.tools.javac.api.JavacTool
at net.openhft.compiler.CompilerUtils.reset(CompilerUtils.java:85)
at net.openhft.compiler.CompilerUtils.(CompilerUtils.java:69)
at tide.utility.Automation.compile(Automation.java:176)
at tide.utility.Automation.Generate(Automation.java:114)
at appstart.TestNgRunner.main(TestNgRunner.java:22)
Caused by: java.lang.ClassNotFoundException: com.sun.tools.javac.api.JavacTool
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at net.openhft.compiler.CompilerUtils.reset(CompilerUtils.java:81)
... 4 more

Batch File
cd
start cmd.exe /k java -jar fullPack.jar "Node01"

Can you please assist me?

@dpisklov
Copy link
Contributor

Are you using CompilerUtils.addClassPath as suggested in comments in #46?

@umeshshende
Copy link
Author

Yes but same result
try { String fileContents = getMethodString(newClassName); CompilerUtils.addClassPath("C:\\Program Files\\Java\\jdk1.8.0_241\\lib\\tools.jar"); CompilerUtils.CACHED_COMPILER.loadFromJava(className, fileContents); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); return false; } return true;
While exception is being getting from this
private static void reset() { s_compiler = ToolProvider.getSystemJavaCompiler(); if (s_compiler == null) { try { Class<?> javacTool = Class.forName("com.sun.tools.javac.api.JavacTool"); Method create = javacTool.getMethod("create"); s_compiler = (JavaCompiler) create.invoke(null); } catch (Exception e) { throw new AssertionError(e); } } }

@dpisklov
Copy link
Contributor

Well I don't know what can be wrong on Windows - no one over here uses that parody on the OS... But may be you need to check if:

  • The path you specified indeed exists (correct version, accessible etc)
  • Try using *nix-style path, i.e. "C:/Program files/..."
  • Instead of CompilerUtils#addClassPath() try adding tools.jar to the classpath
  • If nothing helps post here proper stack trace and reproducible example, although we will only be able to reproduce it on Linux.

@umeshshende
Copy link
Author

I got the solution click here need to copy tools.jar to jre/lib/ext folder thank you :)

@dpisklov
Copy link
Contributor

Well the accepted solution on Stackoverflow is the correct one. Copying a jar is a hack, not a proper solution. Proper solution is to set your JAVA_HOME to point to JDK folder and to correct your PATH to use java from the $JAVA_HOME/bin, as currently it clearly uses the JVM and not JDK.

@umeshshende
Copy link
Author

But another issue is target machine has JDK 14 but in my local machine is jdk8 so it's working but for JDK 14 I need upgrade the version but tool.jar is removed in jdk14 any suggestions on this

@dpisklov
Copy link
Contributor

dpisklov commented Sep 29, 2020 via email

@umeshshende
Copy link
Author

umeshshende commented Sep 30, 2020

That mean package will not on java 14 ? any alternative suggestion? What option do i have ?

@dpisklov
Copy link
Contributor

Actually tools.jar was removed in Java 9, but this project works fine with Java 9 and Java 11. Which means if instead of hacking things by copying jars you do it properly by fixing the JAVA_HOME and PATH, it will work fine. See e.g. here for details on tools.jar

@umeshshende
Copy link
Author

Ok thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants