I've got this working run configuration in IntelliJ which I want to convert tro a command line one. Been trying few things but it can't seem to locate my class file.
Can someone suggest the correct command to run please?
Many thanks
The easiest way is to run this configuration from within IDEA, open the "Run" tool window and copy the command from there.
Note that IDEA adds some specific settings and you'll have to remove them, like removing idea_rt.jar from classpath, etc.
Related
I have a working Spring 5 application which works great. BTW, it is Spring 5 CLASSIC, and does NOT use Spring Boot. So, it is not using an embedded Tomcat. I built a traditional WAR file, and deployed it to an existing Tomcat installation.
This project is a multi-maven module project that is made up of several jars, and ending in one WAR file. This runs great. What I am trying to do is execute one class in one of these jars as a standalone piece of code, it's a utility which I want to run nightly. I know it runs in Eclipse just fine, so I know the code is ok.
Based on the readings here, it is very obvious what has to be done.
java -cp SomeJava.jar com.tomholmes.products.myproject.server.util.MyClassUtil
Yes, this 'MyClassUtil' does indeed have a "main" method as you would expect.
public static void main(String[] args) throws Exception {
TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
MyClassUtil myClassUtil = new MyClassUtil();
myClassUtil.processBackLog();
}
I have deployed a the WAR file (MyProject.war) is deployed on my linux box, and tomcat is located at: /opt/tomcat the war file is deployed to: /opt/tomcat/webapps
and when we restart tomcat, we have:
/opt/tomcat/webapps/MyProject
/opt/tomcat/webapps/MyProject/META-INF
/opt/tomcat/webapps/MyProject/WEB-INF
/opt/tomcat/webapps/MyProject/WEB-INF/classes <--- no files at all
/opt/tomcat/webapps/MyProject/WEB-INF/libs <--- contains all JAR files including
the one that has my class in it
/opt/tomcat/webapps/MyProject/WEB-INF/libs/my-utils.jar <--- contains the class I want to
execute
I have a script file 'my_util.sh' that looks like this.
export JAVA_ROOT=/opt/java
export JAVA_HOME=/opt/java
export JAVA_BINDIR=/opt/java/bin
export JRE_HOME=/opt/java/jre
export JRE_BINDIR=/opt/java/jre/bin
export PATH=${JAVA_HOME}:${JAVA_BINDIR}:$PATH
export PATH=${JRE_HOME}:${JRE_BINDIR}:$PATH
export PATH=${JAVA_HOME}:${JAVA_BINDIR}:$PATH
datestr=$(date +%Y%m%d_%H%M%S)
export CLASSPATH=/opt/tomcat/webapps/MyProject/WEB-INF/libs/my-utils.jar
export MAINCLASS=com.tomholmes.products.myproject.server.util.MyClassUtil
java -cp ${CLASSPATH} ${MAINCLASS}
To which I get the response:
Error: Could not find or load main class com.tomholmes.products.myproject.server.util.MyClassUtil
Based on the past messages here, of which there are many, and I did research, I should have had this resolved by now. And the way I setup everything should be working, but it is not. I am sure I am missing something small, and I feel like I am almost there. If anyone can make a suggestion or help me out with this, that would be great. I'll keep looking for the answer myself to see if I can get it before this question gets answered.
Thanks!
I just figured this out. As I said, the code itself works great from within Eclipse and is unit tested. When you run a class within Eclipse, you can run a Java Class as a standard Java application, and it will create a Configuration for you with a setup classpath. There is even a button that says "Show Command Line" which showed the full classpath, call to java, any -D arguments, and the actual class.
So, there were two things that I missed:
1) I needed to include ALL the jars that were associated with this class. I know that was mentioned in the comments, and I had tried that because I suspected it was the case.
2) However, what I messed up on was the separator between jars. I tried comma (,) and semi-colon (;) before figuring out that I needed the colon (:). Looking the command-line that Eclipse put together clued me in on that.
It was asked, did I try to execute this on the command-line before I did the script, and the answer is: I did not. I just thought I'd go to the script directly.
BTW ... I figured that having some standalone utilities with my overall web-application wasn't a huge problem. It wasn't worth it to create a whole new project for one utility.
Now that I know this script runs, now I can put it on a cron job.
Thanks for the help. And I hope this helps someone else.
I have not been able to find a clear answer on Google.
What happens if an unhandled exception is thrown during runtime by an executable jar file? Will it open CMD? Also, would (exception_name).printStackTrace() do the same?
I would test these things, but I do not know how to create an executable .jar. So a short explanation on how to create one in Eclipse would also be appreciated.
(Easy to lookup and found)
What happened when you put together an example jar and tried yourself?
They'll go to the standard error output. If you don't set it programatically, then it depends on how you're running the jar.
From an IDE it goes to your IDE's output.
If you run the jar from the command line, then that command line is your standard out.
If you run the jar by double-clicking it, then the standard output isn't shown anywhere. It won't automatically open a command prompt, which you would have seen if you just created an example program.
See also, possible duplicate: Java jar output. where it goes?
Edit: Another possible duplicate: Where is System.err on Windows?
When you run a file, it opens the console window and all the way on top it has the directory of where the file is. This is pretty irritating because now, to separate other lines from mixing with the directory, I have to use "\n" on before any System.out.println() commands can be initiated.
"C:\Program FIles..." I want to get rid of that.
There is presently no way to disable this.
It does show useful information which shows which jvm is being used and which parameters are used to configure it, so it does "annotate" your program log to some extent. You'll also get a line stating the exit code when your program terminates.
There is an option to be able to write the console log to a file, I'm not sure if it shows the jvm version and exit code or not, perhaps you can try it and see if it is be useful to you.
Maybe try Settings->Editor->General->Console, in the "Fold console lines that contain:", add a substring of the line you want folded e.g. "C:\Program Files
I've been using Eclipse as my Java IDE and today I ran into an interesting problem. I used the built in file>export>runnable jar file to create a jar file for one of my programs. It runs perfectly if I start it from the command line/with a batch file, but doesn't run from a double click. I made absolutely certain that .jar files are associated with javaw and it still didn't work. It isn't a huge problem I absolutely NEED fixed, but if anyone has any ideas I'd love to hear them.
Update: I tried using Aram Kocharyan's solution (see answer below). No luck. It seems to be just this one .jar file too. All other jars launch with a double click. Rebuilding the jar doesn't help. :P
I wrote a short guide on this a while back you might find helpful:
http://ak.net84.net/projects/how-to-make-a-multi-platform-executable-java-jar-file/
This will work on double click in windows without setting any additional settings, and I've tested it with the Jar Launcher on Mac and it doesn't complain.
open your jar with..javaw
which is in bin folder of jre... in my computer it is #
C:\Program Files\Java\jre\bin\javaw.exe
Put this inside a .bat:
start javaw -classpath "%~dp0YOU-JAR-NAME.jar" -Djava.library.path="native" foo.package.bar.YourClassWithMainMethod
the option -Djava.library.path="native" its not required for run, i include this because one reason for using a .bat instead do a double click on a .jar is to use JVM-parameters, in my case i need this parameter for my project run.
the %~dp0 part get current directory
I built an application in Netbeans 6.8 and made project.jar file. When I run it, it works only on my computer, but not on any other computer. However, when I made any simple application, that doesnt use any libraries, it works fine on any computer.
Is there any way, how to invoke some error message, where is the problem?
My project use R 2.9.2, so I install this version on other computer and set the System Path variable exactly same. Other libraries listed in lib directory are: AbsoluteLayout.jar,DatePicker-V0.99-2006.09.01.jar,jcommon-1.0.16.jar,jfreechart-1.0.13.jar,jmathplot.jar,JRI.jar,pdf-renderer-1.0.5.jar
Thank you
You don't get any message at all? What do "works" and "not works" look like?
You sound like another person who hasn't taken the time to learn how to do things by hand on the command line without an IDE. I'd recommend doing that. Open a command shell and type in the java -jar -cp ... foo.jar command to run your stuff. The messages you get back will be educational.
Note the -cp command line argument. That's how you add your JARs to the CLASSPATH properly.
I solved this problem as follows, maybe it will help someone.I add 2 paths in PATH system variable:
Start -> Control Panel -> System -> Advanced
Click on Environment Variables, under System Variables, find PATH, and click on it.
In the Edit windows, modify PATH by adding the location of the class to the value for PATH.
you must add both paths, to jri.dll and r.dll, in my case it were these:
C:/Program Files/R/R-2.9.2/bin/;C:/Program Files/R/R-2.9.2/library/rJava/jri/;
I have added these lines already, but with different different slash. So be careful, you must use it / not \ to define path!!!