Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Running class from cmd line best practice?  RSS feed

 
Rj Ewing
Ranch Hand
Posts: 120
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the preferred way to run part of a package from the cmd line? In the past I have just used the main() method in one of the existing classes. However I don't think this is the best way since I need to use multiple classes from my codebase.

What is the preferred way to do this?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66187
149
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No problem. Just make sure all the classes your program needs are in the classpath. The main() method is only needed in the first class that gains control when executed.
 
Carey Brown
Bartender
Posts: 2991
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I usually create a Windows batch file (.bat, .cmd) to go along with the Java distribution. On *nix systems I create shell scripts. So, on the command line I type the name of the batch file and let it launch the Java program.
 
Campbell Ritchie
Marshal
Posts: 55681
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As long as all the other classes are accessible from the location of the main method, the JVM will be able to find them; there is a program called a class loader (and there are ClassLoader classes) which do that. Obviously all the classes must be compiled first.
 
Campbell Ritchie
Marshal
Posts: 55681
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:I usually create a Windows batch file (.bat, .cmd) . . . On *nix systems I create shell scripts. . . .
Good idea which will allow you to compile multiple classes simultaneously, presumably.

Would you show us an example of such a file, please.
 
Carey Brown
Bartender
Posts: 2991
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Carey Brown wrote:I usually create a Windows batch file (.bat, .cmd) . . . On *nix systems I create shell scripts. . . .
Good idea which will allow you to compile multiple classes simultaneously, presumably.

Would you show us an example of such a file, please.


filefind.bat

@echo off
java -classpath c:\bin\app_file\libs\csbutils.jar;c:\bin\app_file\filefind.jar FileFind %*
 
Campbell Ritchie
Marshal
Posts: 55681
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you

That means you stop the command line echoing its instructions, and execute the java command to run the main method in the FindFile class using scbutils and findfile .jar files in the classpath.
I found that %* means all the command line arguments
That Last Link wrote:does %* in batch file mean all command line arguments?

. . .

Yes. According to the official Microsoft documentation:

The %* batch parameter is a wildcard reference to all the arguments, not including %0, that are passed to the batch file.
 
Rj Ewing
Ranch Hand
Posts: 120
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess I wasn't questioning whether it would run, more of what is the best practice for this. Here is my class to give more insight.

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!