-1

I'm new to java and I need to run some compiled code

The source

public class OiosiRaspClient {

    private static Log log = LogFactory.getLog(OiosiRaspClient.class);
    public static final String PATH_INVOICE_OIOUBL = "./Resources/xml/OIOUBL_Invoice_v2p1_5798009811578.xml";

    public static final String RaspConfigurationLive = "./Resources/RaspConfiguration.Live.xml";
    public static final String RaspConfigurationTest = "./Resources/RaspConfiguration.Test.xml";

    public UddiType uddiType;
    public String xmlDocumentUrl;

    public enum UddiType {
        Production(RaspConfigurationLive),
        NewProduction(RaspConfigurationLive),
        Test(RaspConfigurationTest),
        LocalJava_v1_2_3(RaspConfigurationTest),
        LocalJava_v1_2_4(RaspConfigurationTest),
        LocalNet_v1_2_3(RaspConfigurationTest),
        LocalNet_v1_2_4(RaspConfigurationTest),

        FOCES2Test("cfg/RaspConfiguration.Oces2.xml"),
        FOCES2_99018008("cfg/RaspConfiguration.99018008.xml"),
        FOCES2_99018080("cfg/RaspConfiguration.99018080.xml");

        private final String raspConfigurationFile;

        UddiType(String configFile) {
            raspConfigurationFile = configFile;
        }
    }

    public OiosiRaspClient() {
        uddiType = UddiType.Production;
        xmlDocumentUrl = PATH_INVOICE_OIOUBL;
    }

    public OiosiRaspClient(UddiType uddiType, String xmlDocumentUrl) {
        this.uddiType = uddiType;
        this.xmlDocumentUrl = xmlDocumentUrl;
    }

    public static void main(final String[] args) {
        new OiosiRaspClient().sendDocument_Test();
    }

    public boolean sendDocument_Test() {
        // some code
    }
    
}

All the code is compiled with ant compile and a build.xml file

Now the class OiosiRaspClient is compiled to the following files

OiosiRaspClient$1.class
OiosiRaspClient$UddiType.class
OiosiRaspClient.class

How to run the main method in OiosiRaspClient ?

I also need to change the public variable PATH_INVOICE_OIOUBL before each run..

command line

# java OiosiRaspClient
Error: Could not find or load main class OiosiRaspClient
Community
  • 1
  • 1
clarkk
  • 27,151
  • 72
  • 200
  • 340

3 Answers3

0

To compile and run OiosiRaspClient use

javac OiosiRaspClient.java
java OiosiRaspClient

or as Nicholas Kadaeux put it so well here

Let's say your file is in C:\mywork\

Run Command Prompt

C:> cd \mywork

This makes C:\mywork the current directory.

C:\mywork> dir

This displays the directory contents. You should see filenamehere.java among the files.

C:\mywork> set path=%path%;C:\Program Files\Java\jdk1.5.0_09\bin

This tells the system where to find JDK programs.

C:\mywork> javac filenamehere.java

This runs javac.exe, the compiler. You should see nothing but the next system prompt...

C:\mywork> dir

javac has created the filenamehere.class file. You should see filenamehere.java and filenamehere.class among the files.

C:\mywork> java filenamehere

This runs the Java interpreter. You should then see your program output.

If the system cannot find javac, check the set path command. If javac runs but you get errors, check your Java text. If the program compiles but you get an exception, check the spelling and capitalization in the file name and the class name and the java HelloWorld command. Java is case-sensitive!

Community
  • 1
  • 1
Dan
  • 7,286
  • 6
  • 49
  • 114
0

I hope I understood your problem, but i think what you need is commandline args. When you run the class' main method with "java RaspiClient C:\Users\..." you can acces that passed argument in the main method like String arg = args[0]

  • Could you show an example of the main method how to override the variable `PATH_INVOICE_OIOUBL` when passing the variable as an argument? – clarkk Dec 23 '15 at 12:06
  • I've created a gist that shows what needs to be changed (note the comments):https://gist.github.com/danielr1996/bb6d3942ef19bedb616e –  Dec 23 '15 at 14:18
0

Lets say you have two class files,

SampleClass1.java and SampleClass2.java (both are in some directory /usr/XYZ/JavaCode/src)

You need to navigate to this directory through command line. When you get to the directory use following command to compile.

javac -d ../bin SampleClass1.java

(bin is the directory at the same level of src where we store the compiled .class files, we need to create that at /usr/XYZ/JavaCode/bin)

And assuming that SampleClass1.java contains a main method, to run it use following command.

java -cp ../bin SampleClass1

EDIT: In order to pass arguements,

java -cp ../bin SampleClass1 "Hello World" Java

In the above example Hello World would be one argument i.e. when you want to pass a sentence pass it using double quotes, and second argument would be Java, a single word can be passed without double quotes.

Access them in the code using arg[0], arg1 ... (or whatever the name you specify in the main method argument not necessarily be arg).

Please note that the class name would be "Fully Qualified Class name" i.e. if you know about package in java you would understand Fully Qualified name, in very short it is like PACKAGE_NAME.CLASSNAME.

Now this to work the priority is to define JAVA_HOME environment variable (Which I guess you have already done otherwise it won't be compiling.)

One more thing about Class, is you can define as many classes in a single file as you want to but only one of them would be public class, and the file name should match with the Public class name.

Further more for you to learn following links seems a right place to start, welcome to the Java world... :) :) Happy learning... :)

First Cup Of Java

Sevle
  • 3,109
  • 2
  • 19
  • 31
Bilbo Baggins
  • 2,899
  • 10
  • 52
  • 77
  • The java files already are compiled.. I just need to run the class file.. But first I need to set the public variable `PATH_INVOICE_OIOUBL` before running the `main` method in `OiosiRaspClient` – clarkk Dec 23 '15 at 11:47
  • didn't get you, you mean for searching for a file at particular location? – Bilbo Baggins Dec 23 '15 at 12:08
  • I just need to send the filepath as an argument to the command line `java .. filepath` – clarkk Dec 23 '15 at 12:11
  • and how would the main method looks like when the arg[0] is overriding the public variable in the class – clarkk Dec 23 '15 at 12:12
  • ok, I am editing it in my answer. – Bilbo Baggins Dec 23 '15 at 12:19
  • When running the command `java OiosiRaspClient` returns this error `Error: Could not find or load main class OiosiRaspClient` – clarkk Dec 23 '15 at 12:29
  • This simply means that your java file is undetected by the Java, i.e. it is not in your classpath, please check your path and try again. also try using -cp with that you need to give the path where your class file is residing, try with relative path if can't try with absolute path. – Bilbo Baggins Dec 23 '15 at 12:37