com.cedarsoft.execution.JavaCommandBuilder Maven / Gradle / Ivy
/**
* Copyright (C) cedarsoft GmbH.
*
* Licensed under the GNU General Public License version 3 (the "License")
* with Classpath Exception; you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.cedarsoft.org/gpl3ce
* (GPL 3 with Classpath Exception)
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 3 only, as
* published by the Free Software Foundation. cedarsoft GmbH designates this
* particular file as subject to the "Classpath" exception as provided
* by cedarsoft GmbH in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 3 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 3 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact cedarsoft GmbH, 72810 Gomaringen, Germany,
* or visit www.cedarsoft.com if you need additional information or
* have any questions.
*/
package com.cedarsoft.execution;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.io.File;
import java.lang.String;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
/**
* Class that is able to start a new java process
*
* @author Johannes Schneider ([email protected])
*/
public class JavaCommandBuilder {
@Nonnull
private String javaBin = "java";
@Nonnull
private final List classPathElements = new ArrayList();
@Nonnull
private final String mainClass;
@Nonnull
private final List vmProperties = new ArrayList();
@Nonnull
private final List arguments = new ArrayList();
/**
* Constructor for JavaCommandBuilder.
*
* @param mainClass a String object.
*/
public JavaCommandBuilder( @Nonnull String mainClass ) {
this.mainClass = mainClass;
}
/**
* Getter for the field classPathElements.
*
* @return a List object.
*/
@Nonnull
public List getClassPathElements() {
return Collections.unmodifiableList( classPathElements );
}
/**
* Setter for the field classPathElements.
*
* @param classPathElements a String object.
*/
public void setClassPathElements( @Nonnull String... classPathElements ) {
this.classPathElements.clear();
for ( String element : classPathElements ) {
if ( element == null || element.length() == 0 ) {
continue;
}
this.classPathElements.add( element );
}
}
/**
* Getter for the field javaBin.
*
* @return a String object.
*/
@Nonnull
public String getJavaBin() {
return javaBin;
}
/**
* Returns the classpath
*
* @return the classpath
*/
@Nullable
@Nonnull
public String getClassPath() {
if ( classPathElements.isEmpty() ) {
return null;
}
StringBuilder stringBuilder = new StringBuilder();
for ( Iterator it = classPathElements.iterator(); it.hasNext(); ) {
String classPathElement = it.next();
if ( classPathElement == null || classPathElement.length() == 0 ) {
continue;
}
stringBuilder.append( classPathElement );
if ( it.hasNext() ) {
stringBuilder.append( File.pathSeparator );
}
}
String classPath = stringBuilder.toString();
if ( classPath.contains( " " ) ) {
if ( !System.getProperty( "os.name" ).toLowerCase().contains( "windows" ) ) {
System.err.println( "WARNING: Be carefull! Added \" to the classpath. May be incompatible!" );
}
return '\"' + classPath + '\"';
} else {
return classPath;
}
}
/**
* Getter for the field mainClass.
*
* @return a String object.
*/
@Nonnull
public String getMainClass() {
return mainClass;
}
/**
* addClassPathElement
*
* @param classPathElement a String object.
*/
public void addClassPathElement( @Nonnull String classPathElement ) {
classPathElements.add( classPathElement );
}
/**
* Getter for the field vmProperties.
*
* @return a List object.
*/
@Nonnull
public List getVmProperties() {
return Collections.unmodifiableList( vmProperties );
}
/**
* Setter for the field vmProperties.
*
* @param vmProperties a String object.
*/
public void setVmProperties( @Nonnull String... vmProperties ) {
this.vmProperties.clear();
for ( String vmProperty : vmProperties ) {
if ( vmProperty == null || vmProperty.length() == 0 ) {
continue;
}
this.vmProperties.add( vmProperty );
}
}
/**
* addVmProperty
*
* @param vmProeprty a String object.
*/
public void addVmProperty( @Nonnull String vmProeprty ) {
vmProperties.add( vmProeprty );
}
/**
* addArgument
*
* @param argument a String object.
*/
public void addArgument( @Nonnull String argument ) {
this.arguments.add( argument );
}
/**
* Setter for the field arguments.
*
* @param arguments a String object.
*/
public void setArguments( @Nonnull String... arguments ) {
this.arguments.clear();
for ( String argument : arguments ) {
if ( argument == null || argument.length() == 0 ) {
continue;
}
this.arguments.add( argument );
}
}
/**
* Getter for the field arguments.
*
* @return a List object.
*/
@Nonnull
public List getArguments() {
return Collections.unmodifiableList( arguments );
}
/**
* getCommandLineElements
*
* @return a List object.
*/
@Nonnull
public List getCommandLineElements() {
List elements = new ArrayList();
elements.add( getJavaBin() );
for ( String property : getVmProperties() ) {
elements.add( "-D" + property );
}
String classPath = getClassPath();
if ( classPath != null ) {
elements.add( "-cp" );
elements.add( classPath );
}
elements.add( getMainClass() );
for ( String argument : getArguments() ) {
elements.add( argument );
}
return elements;
}
/**
* Returns the command line as string. This method should only be used
* for debugging purposes.
* For creating a process use {@link #getCommandLineElements()} instead.
*
* @return the command line as string
*
* @see #getCommandLineElements()
*/
@Nonnull
public String getCommandLine() {
StringBuilder stringBuilder = new StringBuilder();
for ( String element : getCommandLineElements() ) {
stringBuilder.append( element );
stringBuilder.append( ' ' );
}
return stringBuilder.toString().trim();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy