My Java app needs 3rd party JARs to run. I can't seem to get it to run from the command line. Its complaining about NoClassDefFoundErrors, despite setting the classpath to what I imagine to be correct. However, when I run it in NetBeans, all is well - it runs as expected. Is there any way to find out the command + arguments NetBeans uses to run my program? This is NetBeans 7.0, BTW.
Should be:
java -jar dist/ProjectName.jar
That uses the META-INF/MANIFEST.MF file in the jar to determine what to set the class-path to.
First, go to project properties > 'run' and select the main class (one with a main method).
Then make 'clean and build' to generate a jar.
and then execute the jar as this
java -jar dist/yourproject.jar
Or you can double click the jar in windows if you have a JRE installed
Choose Run Menu and Clean and Build Project option
Check the output window where you will see one line if text as follows:
To run this application from the command line without Ant, try:
Line below this line will tell you the exact command line which can be used to run this application from command line with absolute path on your machine.
with regards
Tushar Joshi, Nagpur
Related
I was trying to set up compiling a Netbeans project from the command line. The project uses Ant as a build system, so I found this and followed it. The program compiles fine, however, when trying to run the program using java -jar file.jar it terminates, saying no main manifest attribute.
Furthermore, I would also like to know if there is a way to run the ant command for a specific file, like one would in netbeans when right-clicking and selecting Run file, as when running ant jar there is no opportunity to select a file.
If I create Java file by touch command in command line, edit it using nano, compile it by javac, and then run it, all works well.
But if I use eclipse to build the project, and create file in eclipse. I can compile it using javac, but cannot run it in the command line.
it shows
Error: Could not find or load main class
why this happen? is anything wrong?
sounds like all about right path in which you trying to run your class.
did you use packages? did you run it from bin|target|build dir?
please provide commands and paths from which you trying to execute it
EDIT: OK, I think I was being silly. What I really wanted to do was a runnable JAR. I did and now it works.
So I'm trying to run a little program that interacts with some webpages. The program works fine when I launch it from Eclipse but when I export it to a jar file and try to run it from the command prompt I get this error message:
What am I missing here?
I'm assuming in Eclipse you've added the appropriate libraries to your build path. When you run your program through eclipse, it automatically creates a java command including all your libraries in the classpath.
You need to do the same thing
java -cp /path/to/libs -jar muzictest.jar
I wrote a java program that contains many classes in eclipse, it is my first time that I write a code in java and in eclipse too, now I need to run this code on a server that doesn't have eclipse.
Compiling & Running a Simple Program.
This is your best starting point.
Use "File -> Export -> JAR file" wizard to create a .jar file contained your compiled classes. You can then copy this .jar file to your server and use java.exe to run it. Use "java -help" from a command line to get instructions.
Please go through link provided by Andrew
Eclipse is an IDE , it facilitates development but not actually required to run your code, if you want to run your java file in a server somewhere, make sure you have JRE installed in your "server" and in your Path, then run following command in your server's shell environment
javac filename.java
above command will generate a .class file in the same directory, which is actual file that can be executed using following command,
java filename
assuming your code is inside a file called filename.java
I am trying to compile an application from the command line
The programme compiles and runs fine in eclipse, howvever when i goto the command line and use javac to compile my java file i get 23 errors, the majority of which are Cannot Find Symbol, with an arrow pointing to the . in a package name.
Does anyone have any ideas on what i need to do differently?
Thanks
Your classpath is not set up correctly. Look at your Eclipse project in the .classpath file. In there you will find a lot of classpathentry elements. You will need to replicate this for your command line compilation.
To do this manually you must first set your CLASSPATH environment variable to a list of directories (or jar files) containing class definitions.
You can also use a build tool called ant to automate this for you.
I advise against setting the classpath as an environment variable because it is too intrusive (all your java programs will see it).
A command line for compiling a Java app which dependes on Log4j might look like this:
javac -cp C:\dev\mvn\repo\log4j\log4j\1.2.16\log4j-1.2.16.jar AppenderTester.java
If you have multiple classpath entries you need to separate them with a semicolon.
For ease of use you could create a startup script. This can be a simple batch script or a more elaborate ant script (which requires installing ant).
This is only the tip of the iceberg known as 'classpath hell'.
EDIT: you can also take a look at the Eclipse feature 'export runnable JAR', which packs your application together with all its dependencies in a JAR file.