• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Rob Spoor
  • Henry Wong
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh
  • Jj Roberts

How should I buid a project which contains InMemoryJavaCompiler?

 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I use NetBeans 8.1 for developing a Java project. I use InMemoryJavaCompiler in two classes. When I run the project under NetBeans it works correctly. I created a .jar file from the project with When I try to run the .jar file the program does not work. It generates a NullPointerException regarding InMemoryJavaCompiler:

According to https://stackoverflow.com/questions/2543439/null-pointer-exception-while-using-java-compiler-api most probably the ToolProvider.getSystemJavaCompiler() returns null. I checked the java.home in NetBeans with which is:

I tried to add and move up the from in NetBeans and built again the program but it doses not help. On my computer, the java path is.
Could someone suggest me a solution to fix the nullpointerexception error?
 
Master Rancher
Posts: 4250
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What folder is the java.exe program in that you use to execute the jar file?  It needs to be part of the JDK installation.

Is this the same topic: https://coderanch.com/t/737503/java/Building-jar-file-Java-project
 
Saloon Keeper
Posts: 23409
159
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
JAVA_HOME should point here:


It's always the root of the JDK or JRE that you are executing Java from. That is, you run %JAVA_HOME%\bin\java.exe.

As a purely editorial note, I don't recommend that you run the Java compiler from within an application unless you're doing something like creating a build tool like Ant.
 
Beata Szabo-Takacs
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Tim,
I have tried it but I got the error that the is missing. I have the same problem as in: https://stackoverflow.com/questions/49711640/javacompiler-always-null-with-jdk-versions-1-8-0
I need the InMemoryJavaCompiler because I wrote a java Class with StringBuilder and I have to compile it. Here are the details of my purpose: https://coderanch.com/t/725893/java/convert-strings-executable-Java-code#3378151
I do not understand why does not work the .jar file which was developed and built in NetBeans 8.1 IDE? If I understand well NetBeans has compact profiles which contain the required but I do not understand why the "is missing" or the created .jar file does not find after the packing and building.
 
Norm Radder
Master Rancher
Posts: 4250
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What happens when you open a command prompt and enter this command:
"C:\Program Files\Java\jdk1.8.0_151\bin\java.exe" -jar <YOURJARNAME>.jar

I assume that is the path to the java.exe file in the JDK installation.  You need to replace <YOURJARNAME> with the actual jar file name.
 
Beata Szabo-Takacs
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Norm,
Thank you so much for your suggestion. It works correctly. What do you think I should set the path of the environment variables as C:\Program Files\Java\jdk1.8_151\bin\java.exe?
 
Norm Radder
Master Rancher
Posts: 4250
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How do you execute your jar file?  For example by clicking on it and having the OS use its built-in commandline to execute the jar file.  
Or can you build a batch file that has the commandline like I posted in my last post?
 
Beata Szabo-Takacs
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I used cmd and I applied the command line as you posted. It works correctly. But I do not understand why does not work correctly if I just click to the .jar file?
 
Norm Radder
Master Rancher
Posts: 4250
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

why does not work correctly if I just click to the .jar file?


The java.exe command used to execute the jar file must be part of the JDK: where the javac.exe file is located.  
Usually the java.exe command used to execute jar files with a click is part of the installed JRE,  not the JDK.
 
Ranch Hand
Posts: 61
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Beata Szabo-Takacs wrote:I used cmd and I applied the command line as you posted. It works correctly. But I do not understand why does not work correctly if I just click to the .jar file?


Because that is handeled by the JRE, not the JDK.
Why? As back in the days of Java8 when you installed a JDK you actually also installed an additional regular JRE along with it. That's why you get two folders when installing a JDK8.
The JDK installation pretty much does nothing to the system than extracting the JDK, it does neither register any system wide file-extension information nor did it install the browser plugin. This all happens by the additional JRE installation.
That's why the file-extension .jar gets linked to the command javaw -jar %1 which uses the JRE. And as only up to recent versions JRE and JDK where two separate installations executing something in the context of just a JRE does not provide anything from the tools, like the compiler.

Overall: I still doubt that using a compiler in that way really is what you want to do. If you want to develop Java you're on a good way using Netbeans. But whatever it is you try to achieve using some once internal stuff - the way you try to do it doesn't look like an approach I would come up with. But maybe I just missed something important in the past that justifies it.
As an overall advice: Why stick to Java8? I would recommend upgrade to at least Java11 as that's the current LTS version.
 
Tim Holloway
Saloon Keeper
Posts: 23409
159
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Beata Szabo-Takacs wrote:Dear Norm,
Thank you so much for your suggestion. It works correctly. What do you think I should set the path of the environment variables as C:\Program Files\Java\jdk1.8_151\bin\java.exe?



I repeat. Set JAVA_HOME to


Then add %JAVA_HOME%\bin to your PATH.

That's all you need, other than making sure that all of your application classes are in the CLASSPATH.

As Matthew said indicated, a JDK includes a copy of a JRE inside itself. However, you don't have to do anything to use that JDK, since the JDK will handle that part. Definitely don't put the JRE's bin directory in your PATH! You'd only do that for a stand-alone JRE installation  (without the JDK).

Java has very little interaction with the Windows Registry. It's designed to run primarily from an UNZIPPED JDK or JRE download. In fact, I've never really figured out what Java's registry entries are good for, since no other OS does anything with a "registry" at all. Unix/Linux don't even have a "registry" as such.

However, if you want to click on a JAR and run it, there is one Windows Registry setting you'll need, and that's the one that connects the Windows Desktop to an application based on its file extension. In this case ".jar". It's done the same way that, for example, .xlsx files are registered to open with Excel.
 
Norm Radder
Master Rancher
Posts: 4250
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

what Java's registry entries are good for,


In older versions of the JRE, the registry had entries that pointed to parts of the JRE.  If you tried to use a different version of the java.exe program, you would get a message about some parts not being found.  I have often had multiple versions of java and had many issues with them when I installed a new version of the JRE.  The  installation of a current version of JRE doesn't mess things up like the older ones did.
I have often gone into the Windows Registry to fix problems with a JRE installation.  Lately I have not had to do it.

There is an entry for .jar files  (on my system) that contains a full commandline to where the java.exe file is located.  Since I have often changed it, I have no idea what the current entry for a jar file looks like.  I do not think any of the Environment variables' values are used. So changing PATH, etc will not change what happens when a user clicks on a jar file and the OS uses what is in the registry to execute the jar file.  

More current and standard installation information would be appreciated.
 
Tim Holloway
Saloon Keeper
Posts: 23409
159
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh Yuck. One of the core principles of Java was supposed to be that unlike, say, Internet Explorer, you could have - and run - multiple versions of Java on an OS at any time. I'm not even sure why they had a problem, since the non-Windows versions of Java have never needed outside help. Good to see it's fixed now.

I learned a long time ago that regardless of what OS you're using, it's always safest to use an explicit path for executables, so a registry entry for executing a JAR should be something like "C:\Program Files\Java\jdk1.8.0_151\bin\java.exe ??", where "??" is the magic token that indicates the path of the file that the icon references. Since I don't have a Windows system handy to check, I used "??", but of course, it's actually something like %F or %p or something.
 
Norm Radder
Master Rancher
Posts: 4250
38
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
FWIW I just brought up a Win7 system where I do not think I have made any changes to the registry for java.  Using RegEdit for jarfile I got this:


Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\jarfile]
@="Executable Jar File"

[HKEY_CLASSES_ROOT\jarfile\shell]

[HKEY_CLASSES_ROOT\jarfile\shell\open]

[HKEY_CLASSES_ROOT\jarfile\shell\open\command]
@="\"C:\\Program Files\\Java\\jre6\\bin\\javaw.exe\" -jar \"%1\" %*"


Notice the full path to the javaw.exe file.  I think each new installation of a JRE completely replaced the above entry.

 I have several tools for working with jar files that I had placed entries in the registry for jarfile so that I could RightClick on a jar file and run my tool instead of opening the jar file with the javaw.exe program.  Every new JRE install wiped all my entries and I had to rebuild the jarfile entry in the registry.  I have since worked out a way to protect my stuff.
 
If I'd had more time, I would have written a shorter letter. -T.S. Eliot such a short, tiny ad:
SKIP - a book about connecting industrious people with elderly land owners
https://coderanch.com/t/skip-book
reply
    Bookmark Topic Watch Topic
  • New Topic