Method: Bee::Task::Java#java

Defined in:
lib/bee_task_java.rb

#java(params) ⇒ Object

Run a Java class.

  • main: main Java class to run.

  • classpath: the classpath as defined on javac command line (optional, if dependencies not set).

  • dependencies: the list of files to include into classpath (optional, if classpath not set).

  • jar: Jar file to launch.

  • server: run server version of the VM, must be true or false (defaults to false).

  • properties: system properties to pass to virtual machine, must be a hash, such as { foo: bar } (optional).

  • assertions: enables assertions, must be true or false (defaults to true).

  • verbose: tells if we should generate verbose output, must be true or false (defaults to false).

  • options: custom options to use on command line (optional).

  • arguments: arguments to pass on Java program command line.

Example

- java.java:
    main: test/Test
    classpath: build/classes


218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
# File 'lib/bee_task_java.rb', line 218

def java(params)
  # check parameters
  params_desc = {
    :main         => { :mandatory => false, :type => :string },
    :classpath    => { :mandatory => false, :type => :string },
    :dependencies => { :mandatory => false, :type => :string_or_array },
    :jar          => { :mandatory => false, :type => :string },
    :server       => { :mandatory => false, :type => :boolean,
                       :default => false },
    :properties   => { :mandatory => false, :type => :hash },
    :assertions   => { :mandatory => false, :type => :boolean,
                       :default => true },
    :verbose      => { :mandatory => false, :type => :boolean,
                       :default => false },
    :options      => { :mandatory => false, :type => :string },
    :arguments    => { :mandatory => false, :type => :string }
  }
  check_parameters(params, params_desc)
  main         = params[:main]
  classpath    = params[:classpath]
  dependencies = params[:dependencies]
  jar          = params[:jar]
  server       = params[:server]
  properties   = params[:properties]
  assertions   = params[:assertions]
  verbose      = params[:verbose]
  options      = params[:options]
  arguments    = params[:arguments]
  error "jar 'classpath', 'dependencies' or 'jar' parameters must be set" if
    not classpath and not dependencies and not jar
  # build classpath
  error "Only one of classpath or dependencies may be set" if
    classpath and dependencies
  path = classpath if classpath
  path = build_classpath(dependencies) if dependencies
  # run command line
  if main
    puts "Running java class '#{main}'"
  else
    puts "Running jar file '#{jar}'"
  end
  command = "java "
  command += "-server " if server
  if properties
    for key in properties.keys
      command += "-D#{key}=#{properties[key]} "
    end
  end
  command += "-enableassertions " if assertions
  command += "-verbose " if verbose
  command += "#{options} " if options
  command += "-jar #{jar} " if jar
  command += "-classpath #{path} " if path
  command += "\"#{main}\"" if main
  command += " #{arguments}" if arguments
  puts "Running command '#{command}'" if @verbose
  ok = system(command)
  error "Error running java" unless ok
end