Compile and run java programs in different directroy

Hi,
I often encounter many problems when I run java programs in the different directories. Like
javac -d dir_name a.java
java -cp dir_name a
Something wired often happens, such as, there is not a.java file in some directory, but javac -d dir_name a.java can still work or "java -cp dir_name a" often doesn't work. Moreover, file_name.jar is also a tough problem and solving compiling problems is often time-consuming.
So I hope to read something about how to compile and run java programs in different directory properly.
Could you pleae give me a detailed description for that or recommend a book or website?
Thanks a lot.

Can you post a small amount of code that does not work, including the directory structure, and the error messages? Some one here can likely explain the problem.

Similar Messages

  • How to Set up the  variables and others to compile and Run Java Programs

    Hello,
    I have just downloaded the jdk1.6.0_07 and jre1.6.0_07 and installed it in C:\Program files\Java in my Windows XP ,So please tell me how to sett up the enviroment variables etc to compile and run Java Programs from Command Prompt.
    thanks

    To set the PATH permanently, add the full path of the jdk1.6.0_<version>\bin directory to the PATH variable. Typically this full path looks something like C:\Program Files\Java\jdk1.6.0_<version>\bin. Set the PATH as follows on Microsoft Windows:
    1. Click Start > Control Panel > System on Windows XP or Start > Settings > Control Panel > System on Windows 2000.
    2. Click Advanced > Environment Variables.
    3. Add the location of bin folder of JDK installation for PATH in User Variables and System Variables. A typical value for PATH is:
    C:\Program Files\Java\jdk1.6.0_<version>\bin

  • Compile and run java programs using batch file

    i am using eclipse to run my java programs.How to compile and run those programs using a batch file?

    a) just write a batch file, and add it to the project. When you want to run it, go to a command window and invoke it. (There is probably also a way to invoke it from Eclipse)
    b) if the project is complicated, take a look at ant. Eclispe knows about ant files.

  • How to compile and run java program at command console

    hi there
    can anyone tell me how to compile and run a java program at command console? I have installed JRE 1.3.1, and also have installed JBuilder 5 if it helps.

    try this
    System.out.println("Enter your Name : ");
    BufferedReader console = new BufferedReader( new InputStreamReader( System.in ) );
    String s = console.readLine();
    System.out.println("Hello : "+ s+" !" );

  • How do I compile and run Java programs on a Mac?

    To clarify my earlier post, I know that you can write java in any text editor but with the PC you compile and run everything through the MS DOS prompt, what is the Mac equivalent?

    See this question :-)
    http://forum.java.sun.com/thread.jsp?forum=54&thread=289574

  • Compiling and running Java programs on my laptop

    I have just bought a new laptop but I can't run and compile Java programs using the javac filename.java command in the command prompt. I know I have to download something but I am not sure what link. I would be grateful if somebody could send me on a URL. My O/S is Windows XP. Can somebody advise please?

    If you just want to run java applications, you just need the JRE: J2SE 5.0 JRE
    If you want to compile java source code, you need the JDK: J2SE 5.0 JDK
    It includes also the JRE. Recommended is also the J2SE 5.0 Documentation that contains the javadoc API that describes all the Java classes.
    To start develeoping I recommend the Java Tutorial: http://java.sun.com/docs/books/tutorial/

  • How to compile and run java files on a mac using command line?

    can someone tell me or link me to some article on how to compile and run java files from command line on a mac? I have mac OS X leopard

    What do you mean by "where to put them" ? What do you want to put anywhere ?
    Have you read Peter's comment in brackets ? Perhaps you have a classpath problem ?
    Edited by: Michael_Knight on Aug 31, 2008 4:23 AM

  • How to  compile and run javacard program

    how to compile and run javacard program "HelloWorld"

    1. Where is save Servlet program?
    2. How to set classpath?
    3. how compile and run?
    http://www.coreservlets.com

  • Compile and run a program with Terminal

    Hello, I used TextWrangler to create a .c file on my desktop. I want to compile and run the program using Terminal. However, I am new to programming and I am not sure how to achieve this. I have Xcode with Command Line Tools downloaded. I have the file "6_19.c" on my desktop currently. Any help would be appreciated.

    Your Desktop probably isn't in your execution path, so you might want to start by moving "6_19.c" from your Desktop to your Home folder. After that, in Terminal, just do
    gcc -o 6_19 6_19.c
    The general format is
    gcc -o <name you want the compiled program to have> <name of the source>.c
    Do not put any .extension on the end of the name you want the compiled program to have. Also note that after "gcc" there's a space, then a dash and the next character is a lower case letter 'o', not a numerical zero. There's a space between the name of the compiled program and the name of the source.
    To run the program, at your terminal prompt type
    ./6_19
    The general format is dot forward slash <name of the compiled program>
    There's no spaces at all.
    Good luck.

  • Help:Compiling and running Java Midlets?

    Hello,
    I am a fresh learner in Java.I am finding it very difficult to compile and run Java Midlets on the Computer system.Also,i will like to tranfer the Sourcecodes to a PDA i.e install it on the PDA and have the application walking well.Please help.Thank you.

    You will get all the detailed steps ( have a look at the ff link) to configure the sun wireless toolkit on a PC , midlet compiling, running , uploading to the Palm PDA and testing it on the device. As for the connection, you can use the hot synch cable or a bluetooth hot synch.
    http://developers.sun.com/techtopics/mobility/midp/articles/palm
    Cheers!,
    Birhanu

  • How to compile and run package programs in Java

    Hi,
    I want to know how to compile and run the package programs in Java using -d. instead of creating the package folder manually.
    eg:
    package Test;
    class test1
    public void disp()
    //Any code;
    I want to compile this without creating the folder ' Test ' manually. that is if we use -d with javac the Test folder will be created automatically. I need the format of -d
    could anyone please help me.
    Thanks in Advance ,
    Ambika

    My program Test.java in F:\Tomcat5\webapps\Ambika\WEB-INF\Classes. I compiled in the format below. I got like this. What should I do for this? But yesterday I compiled like this only, It compiled and the folder com\cert\Test.class is created. Today again I compiled the pgm after deleting the already created folder 'com\cert', I got the error like this.
    I've given my pgm and the thing I've got when I compiled it.
    Test.java
    package com.cert;
    public class Test
         public void display()
              System.out.println("Hai");
    F:\Tomcat5\webapps\Ambika\WEB-INF\Classes>javac -d F:\Tomcat5\webapps\Ambika\WEB-INF\Classes\Test.java
    javac: no source files
    Usage: javac <options> <source files>
    where possible options include:
    -g Generate all debugging info
    -g:none Generate no debugging info
    -g:{lines,vars,source} Generate only some debugging info
    -nowarn Generate no warnings
    -verbose Output messages about what the compiler is doing
    -deprecation Output source locations where deprecated APIs are u
    sed
    -classpath <path> Specify where to find user class files
    -cp <path> Specify where to find user class files
    -sourcepath <path> Specify where to find input source files
    -bootclasspath <path> Override location of bootstrap class files
    -extdirs <dirs> Override location of installed extensions
    -endorseddirs <dirs> Override location of endorsed standards path
    -d <directory> Specify where to place generated class files
    -encoding <encoding> Specify character encoding used by source files
    -source <release> Provide source compatibility with specified release
    -target <release> Generate class files for specific VM version
    -version Version information
    -help Print a synopsis of standard options
    -X Print a synopsis of nonstandard options
    -J<flag> Pass <flag> directly to the runtime system
    F:\Tomcat5\webapps\Ambika\WEB-INF\Classes>
    Plz help me.
    thanks in advance
    Ambika

  • How to compile and Run servlet Program

    Hi,
    pls help me the following doubt
    i am using Tomacat 4.1 server.
    1. Where is save Servlet program?
    2. How to set classpath?
    3. how compile and run?
    bye
    Mathi

    1. Where is save Servlet program?
    2. How to set classpath?
    3. how compile and run?
    http://www.coreservlets.com

  • Compiling and Running Swing Programs - can't execute tutorial

    Hi,
    Help is appreciated! :)
    I'm new to java and I'm following [this tutorial|http://java.sun.com/docs/books/tutorial/uiswing/start/compile.html] (I've pasted the code below after the error message.)
    I'm on Opensuse 11 / KDE4. Here's my output for java -version
    java version "1.6.0_06"
    Java(TM) SE Runtime Environment (build 1.6.0_06-b02)
    Java HotSpot(TM) Server VM (build 10.0-b22, mixed mode)
    I pasted the code in an editor, saved it in a temp directory as HelloWorldSwing.java
    In the console at the temp directory, I compiled without problems with "javac HelloWorldSwing.java"
    Two .class files are generated, HelloWorldSwing$1.class & HelloWorldSwing.class
    The tutorial says I can then run the app with "java HelloWorldSwing", but I get the following error.
    Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorldSwing (wrong name: start/HelloWorldSwing)
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:620)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:124)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:260)
    at java.net.URLClassLoader.access$000(URLClassLoader.java:56)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:195)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:276)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
    at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
    package start;
    * HelloWorldSwing.java requires no other files.
    *import javax.swing.*;       
    public class HelloWorldSwing {
         * Create the GUI and show it.  For thread safety,
         * this method should be invoked from the
         * event-dispatching thread.
        private static void createAndShowGUI() {
            //Create and set up the window.
            JFrame frame = new JFrame("HelloWorldSwing");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            //Add the ubiquitous "Hello World" label.
            JLabel label = new JLabel("Hello World");
            frame.getContentPane().add(label);
            //Display the window.
            frame.pack();
            frame.setVisible(true);
        public static void main(String[] args) {
            //Schedule a job for the event-dispatching thread:
            //creating and showing this application's GUI.
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    createAndShowGUI();
    }

    stringchopper wrote:
    I have to execute the file from a parent (project) directory all the time? if the class files are in /temp/start/ , I have to be in /temp and type:
    java start/HelloWorldSwing
    Well, you have to give the fully qualified CLASS NAME. Since your class is in a package named start, you have to specify start/HelloWorldSwing because that is your class' fully qualified name. Java has to manage class namespace and this is part of scheme. Also, while Java can use / or . as the package name separator character, it seems common that people use the period (start.HelloWorldSwing). In my view, this heps re-enforce the fact that it is a class name, not a file path.
    And, I have to have a directory named literally for my package name ("start"... "blah" won't work).
    Yes. Just like you can have different files with the same name in different directories, you can have different classes with the same name in different packages. And just like you can not have files with the same name in the same directory, you can not have the same class name in the same package.
    Why would the tut indicated running from the package directory? Perhaps something else isn't right on my system(?)
    If there is no system Classpath set, the current directory is the default Classpath.
    From the command line, it is common to use the -cp switch.java -cp /usr/myclasses mypackage.MyClass
    The link provided mentioned "echo $CLASSPATH"... mine came up empty. I've previously installed JDK and Netbeans - should I be concerned about how Netbeans will work if I set up a CLASSPATH system var?I can't help you with Netbeans but I believe most IDEs use their own method to find classes.

  • Using different JREs to compile and run JNI program

    Hi,
    Is it possible to use a JRE to compile a JNI program and then use another JRE to run it? For example, I'm compiling my program linking with Sun's libjvm.so and libjava.so. But at run time, I'll change my LD_LIBRARY_PATH to point to IBM's libjvm.so and libjava.so. Would it work at all?

    Oh I forgot to mentioned that I'm trying to invoke JVM in a C program to execute pre-compiled java byte code. (my bad... should've made that clear) So when I say "compile", I mean to compile C files instead of java files. So my original question is: is it possible to compile a C program invoking JVM linking with java and jvm libraries from vendor A and then at runtime, swapping java and jvm libraries with the ones from vendor B? (Since libjava.so and libjvm.so are dynamic libraries, it should work. But I was just making sure...) After some testing, I was able to compile my C program linking with Sun's libraries and to run it with IBM's libraries.

  • Java compilation and running within jsp page

    hi,
    i need of a jsp page that has a textarea in which the java coding are entered and this coding is compiled and executed. The execution result is printed.
    Please guide me.

    I don't think you can invoke java compiler or run time environment at client side (browser). Even if you use applet, i think you will not be able to do it at client side. Moreover, though java virtual machine will be available in your target user's browser, but you should not expect a java compiler in users machine.
    I have seen exam sites where they have added java code compilation and execution ability. To achieve this you can do:
    1. In your JSP page use text area where user can edit java code.
    2. Provide two buttons - 'Compile' and 'Run'.
    3. On click of 'Compile' send the code to server side. In server machine create a file in a temporary location in disk and save the code content received from client.
    4. Invoke 'javac' command (as a separate process) to compile the java class created in step 3. This process will give you error/output back which you can send back to user.
    5. In case of 'Run' you must first invoke above mentioned 'Compile' service and then another service ('Run') to actually run you java class. You can build 'Run' service in similar way.
    Here you challenge would be managing multiple client requests (to compile and run java class) at the same time.
    Code to compile and run java classes:
    import java.io.*;
    * A class to compile and run java classes.
    * @author Mrityunjoy Saha
    public class JavaProcessor{
         public static void main(String[] args){
              // Option 1 to compile and option 2 to run.
              int option=1;
              if(args.length > 0){
                   option=Integer.parseInt(args[0]);
              JavaProcessor p=new JavaProcessor();
              if(option==1){
                   p.compile();
              } else {
                   p.run();
         public void run(){
              String file="FoodTest";
              String directory="C:/Users/mrityunjoy_saha/Documents/Test";     
              executeCommand("java", directory, file);
         public void compile(){
              String file="FoodTest.java";
              String directory="C:/Users/mrityunjoy_saha/Documents/Test";     
              executeCommand("javac", directory, file);
         public void executeCommand(String command, String directory, String file){
              try{
                   String[] envp=null;
                   // Make sure that you have done Java setup in your machine.
                   // To test this try invoking javac command in command prompt.
                   Process p=Runtime.getRuntime().exec(command+" "+file,envp,new File(directory));
                   BufferedReader errorStream=new BufferedReader(new InputStreamReader(p.getErrorStream()));
                   String readData="";
                   while((readData=errorStream.readLine())!=null){
                        System.out.println(readData);
                   BufferedReader outStream=new BufferedReader(new InputStreamReader(p.getInputStream()));
                   readData="";
                   while((readData=outStream.readLine())!=null){
                        System.out.println(readData);
              catch(IOException ioe){
                   ioe.printStackTrace();
    }Thanks,
    Mrityunjoy

Maybe you are looking for