NetBeans Forums

 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister   ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
  

Running Java programs from the command line

 
Post new topic   Reply to topic    NetBeans Forums -> NetBeans Users
View previous topic :: View next topic  
Author Message
ryant



Joined: 22 Oct 2010
Posts: 2

PostPosted: Fri Oct 22, 2010 4:03 pm    Post subject: Running Java programs from the command line Reply with quote

I have a project containing several Java programs which build and run fine within NetBeans 6.9.1. Each program is a separate run configuration in the project properties. They all use many external libraries (Guava, log4j, etc).

How do I run these configurations from the command line outside of NetBeans? Is it possible to take advantage of NetBeans' knowledge of the project's classpath, etc?

It seems like I can sort of run the main class directly using something like

Code:
ant -Drun.class=path.to.my.Program -Djavac.includes= run-single


but I can't pass it arguments on the command line. In addition, every line of output is prefixed with " [java] ". It also takes a long time just to get started since ant verifies first that the build is up to date.

Is it possible to just run the configuration "directly", meaning don't check whether it's compiled or not, just run it, but use the classpath that NetBeans knows?

Thanks for your help,
-Ryan
Back to top
Mark Wade
Posted via mailing list.





PostPosted: Sat Oct 23, 2010 12:21 am    Post subject: Running Java programs from the command line Reply with quote

On Oct 22, 2010, at 12:04 PM, ryant wrote:
Quote:
I have a project containing several Java programs which build and run fine within NetBeans 6.9.1. Each program is a separate run configuration in the project properties. They all use many external libraries (Guava, log4j, etc).

How do I run these configurations from the command line outside of NetBeans? Is it possible to take advantage of NetBeans' knowledge of the project's classpath, etc?

It seems like I can sort of run the main class directly using something like


Code:
ant -Drun.class=path.to.my.Program -Djavac.includes= run-single



but I can't pass it arguments on the command line. In addition, every line of output is prefixed with " [java] ". It also takes a long time just to get started since ant verifies first that the build is up to date.

Is it possible to just run the configuration "directly", meaning don't check whether it's compiled or not, just run it, but use the classpath that NetBeans knows?

Thanks for your help,
-Ryan





I don't know the answers to all those questions but to run those programs from a command line you have to specify a classpath that includes fully qualified paths to each library that is associated with the project you are trying to run, each separated by a semi-colon.


So, perhaps, it might be:


java -cp /home/mydir/javalibs/javalib1.jar;/home/mydir/javalibs2/moreJavaLibs.jar MyWayCoolProgram


or


java cp "C:/Documents and Settings/me/My Documents/java/javaLibs.jar;C:/Documents and Settings/me/My Documents/java/javaLibs2.jar MyWayCoolProgram


Almost always problems executing java programs pertain to the classpath. It can be specified as an environment variable, as a command line entry with -classpath or -cp flags or in a script or batch file.


Mark Wade
address-removed ([email]address-removed[/email])
Back to top
ryant



Joined: 22 Oct 2010
Posts: 2

PostPosted: Sat Oct 23, 2010 11:34 am    Post subject: Re: Running Java programs from the command line Reply with quote

Mark Wade wrote:
On Oct 22, 2010, at 12:04 PM, ryant wrote:
Quote:
I have a project containing several Java programs which build and run fine within NetBeans 6.9.1. Each program is a separate run configuration in the project properties. They all use many external libraries (Guava, log4j, etc).

How do I run these configurations from the command line outside of NetBeans? Is it possible to take advantage of NetBeans' knowledge of the project's classpath, etc?

It seems like I can sort of run the main class directly using something like


Code:
ant -Drun.class=path.to.my.Program -Djavac.includes= run-single



but I can't pass it arguments on the command line. In addition, every line of output is prefixed with " [java] ". It also takes a long time just to get started since ant verifies first that the build is up to date.

Is it possible to just run the configuration "directly", meaning don't check whether it's compiled or not, just run it, but use the classpath that NetBeans knows?

Thanks for your help,
-Ryan





I don't know the answers to all those questions but to run those programs from a command line you have to specify a classpath that includes fully qualified paths to each library that is associated with the project you are trying to run, each separated by a semi-colon.


So, perhaps, it might be:


java -cp /home/mydir/javalibs/javalib1.jar;/home/mydir/javalibs2/moreJavaLibs.jar MyWayCoolProgram


or


java cp "C:/Documents and Settings/me/My Documents/java/javaLibs.jar;C:/Documents and Settings/me/My Documents/java/javaLibs2.jar MyWayCoolProgram


Almost always problems executing java programs pertain to the classpath. It can be specified as an environment variable, as a command line entry with -classpath or -cp flags or in a script or batch file.


Mark Wade
address-removed ([email]address-removed[/email])


I know I can run java manually by specifying the classpath myself, and that's what I've been doing. I have a shell script that basically does:

Code:
java -cp <my-super-long-class-path> "$@"


that I use to run each program. The problem is that this requires a separate shell script for each project and each script needs to be manually updated every time a new library or project is added to the classpath in the project settings.

I recently switched to NetBeans from Eclipse solely to be able to build my project outside of the IDE thanks to the ant build files NetBeans makes for you. I often write code on one machine using the IDE but then need to check out, build, and run the programs on another machine using only the command line. Building is easy with NetBeans but I don't know yet how to make running as smooth.

-Ryan
Back to top
areeda



Joined: 28 Aug 2008
Posts: 485
Location: Los Angeles

PostPosted: Sat Oct 23, 2010 7:31 pm    Post subject: Reply with quote

Another way to do it is to use the properties/library tab to add the class libraries to your project.

Then a clean/build will create a dist directory in your project with a lib directory containing all the libraries.

At that point you can java -jar <path to project>/dist/<prog>.jar arg1 arg2 ....
Back to top
Mark Wade
Posted via mailing list.





PostPosted: Sun Oct 24, 2010 1:36 pm    Post subject: Running Java programs from the command line Reply with quote

On Oct 23, 2010, at 7:35 AM, ryant wrote:

Quote:
I know I can run java manually by specifying the classpath myself,
and that's what I've been doing. I have a shell script that
basically does:


Code:
java -cp <my-super-long-class-path> "$@"



that I use to run each program. The problem is that this requires a
separate shell script for each project and each script needs to be
manually updated every time a new library or project is added to the
classpath in the project settings.

I recently switched to NetBeans from Eclipse solely to be able to
build my project outside of the IDE thanks to the ant build files
NetBeans makes for you. I often write code on one machine using the
IDE but then need to check out, build, and run the programs on
another machine using only the command line. Building is easy with
NetBeans but I don't know yet how to make running as smooth.

What areeda said:

Quote:
Quote:
Another way to do it is to use the properties/library tab to add
the class libraries to your project.

Then a clean/build will create a dist directory in your project
with a lib directory containing all the libraries.

At that point you can java -jar <path to project>/dist/<prog>.jar
arg1 arg2 ....

Mark Wade
address-removed
Back to top
Display posts from previous:   
Post new topic   Reply to topic    NetBeans Forums -> NetBeans Users All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB
By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2012, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo