• Post Reply Bookmark Topic Watch Topic
  • New Topic

Moving code from running from the command line to running within a larger java program  RSS feed

 
Bob Matthews
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I have installed NetBeans and now understand how to run stand alone programs by passing any necessary arguments

Is there a standard way to incorporate such programs into a larger java program ?
(i.e. such that the code is run from within the larger program)

Bob M
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it's hard to answer without seeing what exactly you have, but remember...

your main method is just like ANY other method. You can explicitly call it if you want from another class.

If your class is written correctly, you would create an object of your current class, and you could simply call its methods.

Does that help?
 
Bob Matthews
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Fred

My code starts as follows:-

public class Tokenize {

public static void main(String[] args) {
// read the input file
if(args.length != 1) {
System.err.println("pass the name of the file to be read as an argument");
System.exit(-1);
}
String fileName = args[0];
// use a TreeSet<String> which will automatically sort the words
// in alphabetical order
Set<String> words = new TreeSet<>();

Bob M
 
Jacob Anawalt
Ranch Hand
Posts: 64
Linux Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I like to think of creating objects and see the main method as a way of testing or interacting with them. Here is an example creating an instance of a class and calling it's methods like Fred said:



See how the main method driver gives an example of how one might use MyClass as part of a larger program?
 
Bob Matthews
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jacob

I think I understand..........................
I currently have the following code:-


When I run the large java program I get the following output:-

headlines2: [] ???

Bob M
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bob Matthews wrote:
When I run the large java program I get the following output:-

headlines2: [] ???


Well, were you expecting your tokenizer doIt() method to return an empty set? If not, you will need to debug it to figure out why.

Henry
 
Bob Matthews
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Henry

No I am not expecting an empty string

As I have stated before, the tokenizing method works OK as a standalone in Netbeans

It is when I try the above code that get a problem

Bob M
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!