Setting the JVM to use with Ant when using the command line - java

I have to build a project using Ant from the command line. It is an EAR to be deployed on weblogic 8.1 and as such the classpath is set on the correct jrockit, shipped with the 8.1 server. However, the tool keeps using the jre7 (also installed on my machine), going as far as not even starting if i uninstall the jre7 and leading to problems such as "Cannot find class Object", and so on.
Before running ant, I run the %WEBLOGIC_HOME%/server/bin/setWLSenv.cmd file in the same console session. Both %WEBLOGIC_HOME% and %JAVA_HOME% are correctly set. The jre7, as far as I know, is not referenced in any environment variable nor in any of the build files. I am not permitted to modify the build.xml file, also I can't use Eclipse or any other GUI to build the project.
So, how can I persuade ant to use the correct JVM?

Related

Eclipse Java version not same as compiled Jar

I have a Java Application on Eclipse using jdk1.7.0_72
I am having an issues when running a project from a runnable jar, the application works perfectly when ran from Eclipse. The application uses a cacert which for some reason fails when I use the runnable jar. What I have noticed is that the java version of the runnable jar is different when I run it from Eclipse. This is causing issues.
Here is a snippet of the system properties when I run from Eclipse:
**sun.boot.library.path=C:\Program Files\Java\jdk1.7.0_72\jre...
java.vm.version=24.72-b04
java.runtime.version=1.7.0_72-b14
java.awt.graphicsenv=sun.awt.Win32GraphicsEnvironment
java.endorsed.dirs=C:\Program Files\Java\jdk1.7.0_72\jre…
java.library.path=C:\Program Files\Java\jdk1.7.0_72\bin…**
Here is a snippet of the system properties when I run from JAR:
**sun.boot.library.path=C:\Program Files\Java\jre7\bin
java.vm.version=24.75-b04
java.runtime.version=1.7.0_75-b13
java.awt.graphicsenv=sun.awt.Win32GraphicsEnvironment
java.endorsed.dirs=C:\Program Files\Java\jre7\lib\endorsed
java.library.path=C:\Program Files\Java\jre7\bin;C:\Win…**
How can I make the JAR use the same version that Eclipse uses?
Also when I run java -version I get this:
"1.7.0_72"
When I click on About Java, I get this:
"1.7.0_75-b13"
If you run a JAR it'll use the Java version you set up on the PATH variable. If you run it from Eclipse you can tell Eclipse which version to use. As you can see the library.path variable is different, so you're loading a different Java in each case.
You can either set the proper Java version in the PATH variable or set it in Eclipse in the Preferences - Java - Installed JRE's menu.
It looks like you have at least two Java version installed on your machine.
Java 1.7.0_72-b14 is configured in your Eclipse
Java 1.7.0_75-b13 is configured in your PATH
You can change the java in your environment system variables according to this guide:
https://docs.oracle.com/javase/tutorial/essential/environment/paths.html
You are most definitely using different JRE's.
You have both the JDK and the JRE installed. It just so happens that the JDK has an internal JRE which you can see in the first line:
**sun.boot.library.path=C:\Program Files\Java\jdk1.7.0_72\jre...
The JRE is installed here
C:\Program Files\Java\jre7\bin
Your best option is to point Eclipse to execute your JAR using the installed JRE since the other option is to require the JAR to use the Eclipse JDK/JRE which would be problematic if you wanted to distribute the JAR to machines that didn't have Eclipse installed.
In Eclipse you can designate multiple JRE's and name them, create a new one and give it this path, then you can go to your project's run options and tell it to use this library when running your JAR.
"The java version of the runnable jar" cannot be different because of different execution environments: it's the JVM version.
The issue is that the jar is compiled with an higher version than the execution environment.
In the Eclipse IDE:
in Window/Preferences go to Java/Installed JREs
add your 1.7.0_72-b14 JRE
check the newly added JRE as the default for the workspace
Clean/recompile the project an re-export the jar file with File/Export/jar file

Running Ant Build.xml getting: Java Virtual Machine Launcher: Could not find the main class. Program will exit

I am writing my code in eclipse, the code is running fine but when I try to build it in ant by running build.xml. I am getting
Java Virtual Machine Launcher: Could not find the main class. Program will exit.
Any suggestions?
if you are using java 1.6 and upgraded eclipse, it's more likely caused by eclipse the solution is to go to Run as → External tools configuration… → JRE where the default was set to “Separate JDK”: jdk1.6.0_31 and change this to “run in the same JRE as the workspace” (1.7/1.8), and it should works.
Bug fix would be to provide a working default configuration with setup. Configuration should be in a way it does not break if the user adds additional JDKs to Preferences.
for more details you can see Bug 472599
You need to verify few things
1) Is the path to java bin directory set properly?? (To check this you need to write command javac in command prompt(cmd) and you will get a list of instructions on cmd console) 2) Open the eclipse and go to Window-> Preferences-> Ant-> Runtime in the Classpath tab, see Ant Home Entries pointing to the wrong directory (it still points to the last directory, this directory does not exist) you have to do is to change the Ant Home pointing to the correct directory, to point Ant Home button select Ant Home directory, for example, I use Eclipse 3.3.1, Ant Home is the Eclipse plugin directory org.apache.ant_1. 7.0.v200706080842.
Right click on the build.xml file and select Run As --> External tool configurations --> Jre --> Select Run in the same JRE as the workspace.
If you are using Java 1.5 (that is, it is specified in your workspace, project or ant configuration as the JVM to use), it is also caused by a bug/feature in Eclipse: Java 1.5 is no longer supported. You need to upgrade to Java 1.6.
See: Eclipse bugs 421423
The version of ant you're using may not be compatible with the version of java you are using. I have ant 1.9.4 and was trying to run with java 1.4 and got this same error. Running with java 1.7 fixed it for me!
Error I was facing :
"Could not find the main class :
org.eclipse.ant.internal.launching.remote.internalAntRunner. Program will exit."
To compile my code with JDK 1.8, i have added external ANT to my eclipse, which was working fine. But compiling the code with JDK 1.6 with same ANT version was not working.
Solution : I restored the ANT in to its default value by selecting " Restore Default Entries " ( ANT --> Run as --> External Tools configurations --> classpath" ), after that ANT with JDK 1.6 is working fine.
Above error was happening for me when i added external ANT to my eclipse which was compatible with JDK 1.8 usage. Ant was working with JDK 1.8, the same was not working with 1.6 JDK. Once i restored ANT classpath settings to its default eclipse values which was JDK 1.6 compatable, above error was gone.

how to solve a groovy/grails tool suite reference?

I've been working on my desktop computer which has openjdk 7.
When I copy a Grails project onto my laptop, which has Oracle's JDK v7, I'm having a problem with the classpath.
I've checked the classpath and everything seems fine.
Here's the text that GGTS shows:
The archive: /usr/lib/jvm/java-7-openjdk-amd64/lib/tools.jar which is
referenced by the classpath, does not exist
I set the JAVA_HOME inside /etc/profile.
I installed gvm in order to make easier Groovy and Grails setup.
I hope you could help me
:)
I then went to the RUN menu option at the top and selected run configuration
It was on : Grails \ {Myproject} (run-app)
On the right for this run-app there is JRE options
under JRE ensure you have the same JRE defined as project in this case 1.6
I'm guessing that your JRE reference in your project is bad. Is your desktop windows and your laptop a mac or linux (or vice versa)?
Try these things:
Does the /usr/lib/jvm/java-7-openjdk-amd64/lib/tools.jar path exist on the broken system?
Can you create other kinds of projects on the broken system?
Can you create a new Grails project from scratch inside of the broken GGTS?
What happens if you run Grails -> Refresh dependencies on the project?

Executing Eclipse plugin (jdt/ast) outside eclipse IDE environment

Can I execute Eclipse Java development tools as a standalone process? I need to get Java program's AST structure using Eclipse JDT from another program, and to do that, I need to execute eclipse plugin as a stand-alone process behind the scene.
Is that possible? If so, how one can do that?
Eclipse/jdt and eclipse/ast is nothing more than a jar file, so one can use them to build standalone java application.
Example
I googled to find ASTExplorer as an example to make eclipse/ast plugin as a standalone java project.
The program was targeted for pretty old eclipse (3.0.2), I downloaded the 3.0.2 for Mac OS X this site - http://archive.eclipse.org/eclipse/downloads/drops/R-3.0.2-200503110845/
You need to setup ECLIPSE_HOME classpath variables in Preference -> Java -> Classpath variables
The .classpath has those classpath variables already, you can refer to this post - .classpath contents update in eclipse. As you can see it refers eclipse/jdt(art) jar files. I'm pretty sure one can change the reference to the newest version of jdt/ast without downloading the 3.0.2 version.
As I use Mac, I need to replace the swt for PC with swt for Mac. I could do that in BuildPath/Configure Build path
First remove the PC swt reference.
Then add the correct Mac swt reference.
Compile the example with the eclipse indigo/on mac
When the setup is correct, eclipse starts building the project. You can use Project -> Build Project menu. Then, you can check the application works fine in eclipse.
Generate the executable jar
Export to executable jar file.
You'll get some warnings, but you'll have a jar file.
Execute the generated jar
Just executing java -jar ast.jar doesn't work on Mac, I got a hint from this post.
Running SWT based, cross-platform jar properly on a Mac
In short, you need to run java -XstartOnFirstThread -jar ast.jar
Does it have to be Eclipses's AST? Or is any AST generation o.k.? If it doesn't have to be eclipse, I'd suggest Habelitz open source Java AST Compiler. http://www.habelitz.com/ I'm using it myself for some projects....
Well, of course you can launch eclipse just like any other process : How to create a process in Java
And then the problem becomes relaying the information back to the original process, which gets kind of messy. My suggested approach in this case would probably be to create a plugin in eclipse and have that plugin do whatever it is you think you need the AST for. You can work out messages with command line options to eclipse, or a shared file or something.

Eclipse, java and dotCMS logging issue

I've been assigned the task of tracking down why a webpage is breaking under dotCMS. The page shows an error stack trace with a description that suggests it relates to SQL. I got access to the remote file system and was able to pull up the java file. The sql it is generating is failing when passed to a getResults method. I was wanting to see the value of that string of sql.
Maybe I could create a text file with that value through java. The previous programmer (that left the business) told me that I would need to shut down the server, clean plugins, deploy plugins and re-start the server so the source could be recompiled into jar files. I don't know how to do any of that at this point...and I wonder if those measures should only be taken when I'm deploying the fix.
I know Eclipse has logging abilities because I've dabbled with Android. I didn't know if there is a way that I could somehow remotely connect to the file. I found a tutorial for setting up eclipse with dotcms. It however wants me to check out the official dotcms source to my machine. I guess I don't understand how remote connections work with eclipse and if eclipse could even help with this issue. Noob question for sure!
When you want to run dotCMS from within eclipse it is not that hard. Remember that dotCMS is just tomcat with a default host deployed in it (the dotCMS application). You can download the dotCMS source here: http://dotcms.com/downloads/index.dot. Startup eclipse and add a tomcat server ( It is tomcat 6.0.26 at this moment), pointing the tomcat folder to the tomcat directory in the dotCMS install folder. You can start tomcat from eclipse this way.
You will have to deploy the plugins if you need to debug those. Below I describe it in detail, but dotCMS has a page on this on their official site as well: http://dotcms.com/plugins/plugin-faq.dot#HowInstall
Make sure you have access to the server that dotCMS is deployed on. On windows that would mean a remote desktop, and on linux you'll need ssh. If you have it running in eclipse on your local system then of course you're set already.
After logging in on the server make sure you have apache ant installed on your system. You can check if you have it by typing "ant -version" on the command line (in a dos box on windows). If you have it installed you'll see something like this:
Apache Ant version 1.8.0 compiled on March 11 2010
if you don't have it installed you can download it here: http://ant.apache.org/bindownload.cgi
install ant and make sure you put the /bin folder on the PATH, so you will be able to call ant from all directories
Now check if you have a JAVA jdk installed. You will need a jdk and not only a jre, because building the plugins includes compiling java files into classes. You can check if you have the jdk installed by typing "javac" on the command line. If you don't have it, install the latest java jdk from here : http://www.oracle.com/technetwork/java/javase/downloads/jdk-7u3-download-1501626.html
install the jdk and make sure the /bin folder os on the PATH
Now stop dotCMS
Go to the root of the dotCMS folder and undeploy the plugins using this command:
ant undeploy-plugins
Now build the plugins
ant build-plugins.
And finally deploy them
ant deploy-plugins
Your plugins are redeployed now. You can restart dotCMS.

Categories

Resources