13 Replies - 7235 Views - Last Post: 17 August 2011 - 09:57 AM Rate Topic: -----

#1 sam.bengtson  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 58
  • Joined: 21-February 11

How to compile java project with multiple packages in linux?

Posted 17 August 2011 - 06:37 AM

Hello I am trying to compile a java program in linux that contains multiple classes and packages. I tried using the 'javac' command and specifying my main class but it errors saying that my other classes and imports are missing. How can I compile everything at once so the JDK can find all of my files? Here is the output:
[root@localhost agi-bin]# javac -cp asteriskJava.jar AsteriskConnector/src/Asterisk/Main.java
AsteriskConnector/src/Asterisk/Main.java:3: package javax.xml.rpc does not exist
import javax.xml.rpc.ServiceException;
                    ^
AsteriskConnector/src/Asterisk/Main.java:4: package org.apache.axis does not exist
import org.apache.axis.AxisFault;
                      ^
AsteriskConnector/src/Asterisk/Main.java:5: package org.datacontract.schemas._2004._07.WcfService2 does not exist
import org.datacontract.schemas._2004._07.WcfService2.*;
^
AsteriskConnector/src/Asterisk/Main.java:6: package org.tempuri does not exist
import org.tempuri.*;
^
AsteriskConnector/src/Asterisk/Main.java:7: package port.users.octo does not exist
import port.users.octo.*;
^
AsteriskConnector/src/Asterisk/Main.java:8: package service.users.octo does not exist
import service.users.octo.*;
^
AsteriskConnector/src/Asterisk/Main.java:23: cannot find symbol
symbol  : class UserPortLocator
location: class Asterisk.Main
                UserPortLocator locator = new UserPortLocator();
                ^
AsteriskConnector/src/Asterisk/Main.java:23: cannot find symbol
symbol  : class UserPortLocator
location: class Asterisk.Main
                UserPortLocator locator = new UserPortLocator();
                                              ^
AsteriskConnector/src/Asterisk/Main.java:24: cannot find symbol
symbol  : class UserService
location: class Asterisk.Main
                UserService client = locator.getBasicHttpBinding_UserService();
                ^
9 errors


Is This A Good Question/Topic? 0
  • +

Replies To: How to compile java project with multiple packages in linux?

#2 vzybilly  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 160
  • Joined: 24-September 08

Re: How to compile java project with multiple packages in linux?

Posted 17 August 2011 - 07:30 AM

I've done little with the CLI, I mostly use jGRASP for mine, but Have you tried going from the smallest class up?

in a way so that main is last?

also, do you have everything together? I've seen people try to compile a main for a program in another folder and get something kinda like that...

This post has been edited by vzybilly: 17 August 2011 - 07:32 AM

Was This Post Helpful? 0
  • +
  • -

#3 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1155
  • View blog
  • Posts: 2,534
  • Joined: 05-May 05

Re: How to compile java project with multiple packages in linux?

Posted 17 August 2011 - 08:01 AM

You need to put the necessary jars in your classpath. For instance, org.apache.axis.AxisFault is a class found in axis.jar which can be downloaded here.
Was This Post Helpful? 0
  • +
  • -

#4 sam.bengtson  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 58
  • Joined: 21-February 11

Re: How to compile java project with multiple packages in linux?

Posted 17 August 2011 - 08:07 AM

View Postblackcompe, on 17 August 2011 - 08:01 AM, said:

You need to put the necessary jars in your classpath. For instance, org.apache.axis.AxisFault is a class found in axis.jar which can be downloaded here.


In my eclipse project, these jars are included in the class path. In fact, the project builds and runs just fine in eclipse but as soon as I move to the command line, no such luck. And unfortunately it is a requirement that I am able to run this project from the command line because I will be calling it using a Asterisk AGI application.
Was This Post Helpful? 0
  • +
  • -

#5 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1155
  • View blog
  • Posts: 2,534
  • Joined: 05-May 05

Re: How to compile java project with multiple packages in linux?

Posted 17 August 2011 - 08:13 AM

Well, my answer doesn't change. You need to add their location to the classpath. Eclipse did that for you.
Was This Post Helpful? 0
  • +
  • -

#6 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7804
  • View blog
  • Posts: 13,198
  • Joined: 19-March 11

Re: How to compile java project with multiple packages in linux?

Posted 17 August 2011 - 08:30 AM

Why are you building a complex project without ant?
Was This Post Helpful? 0
  • +
  • -

#7 sam.bengtson  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 58
  • Joined: 21-February 11

Re: How to compile java project with multiple packages in linux?

Posted 17 August 2011 - 08:44 AM

View Postjon.kiparsky, on 17 August 2011 - 08:30 AM, said:

Why are you building a complex project without ant?

I'm not very familiar with ant. If I were to create a build file, do you think this would be easier?
Was This Post Helpful? 0
  • +
  • -

#8 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7804
  • View blog
  • Posts: 13,198
  • Joined: 19-March 11

Re: How to compile java project with multiple packages in linux?

Posted 17 August 2011 - 09:09 AM

I've always found that a good build file makes life easier. I typically have targets for compile, make-jar, test, make-javadoc, and run, among others. Basically, everything that I do with the source from the command line, I do from ant. This then allows me to automate my build process - a build target might run my tests and make me an executable, a build-all target might make me javadoc as well, and a deploy target might do build-all plus commit changes to my repo and put executable and current javadoc where the rest of my team can find it (ie, so a client or a tech writer or other non-dev can run the most current build).
Ant can pretty much do everything but get me another beer and some more popcorn, and I think they're working on that.

In this case, you might use it to avoid putting these jars in your default classpath, and to simplify your compile step, minimizing the typos and brainfarts in the process.
Was This Post Helpful? 2
  • +
  • -

#9 sam.bengtson  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 58
  • Joined: 21-February 11

Re: How to compile java project with multiple packages in linux?

Posted 17 August 2011 - 09:18 AM

View Postjon.kiparsky, on 17 August 2011 - 09:09 AM, said:

I've always found that a good build file makes life easier. I typically have targets for compile, make-jar, test, make-javadoc, and run, among others. Basically, everything that I do with the source from the command line, I do from ant. This then allows me to automate my build process - a build target might run my tests and make me an executable, a build-all target might make me javadoc as well, and a deploy target might do build-all plus commit changes to my repo and put executable and current javadoc where the rest of my team can find it (ie, so a client or a tech writer or other non-dev can run the most current build).
Ant can pretty much do everything but get me another beer and some more popcorn, and I think they're working on that.

In this case, you might use it to avoid putting these jars in your default classpath, and to simplify your compile step, minimizing the typos and brainfarts in the process.

Alright Ill give it a shot. Thanks!
Was This Post Helpful? 0
  • +
  • -

#10 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7804
  • View blog
  • Posts: 13,198
  • Joined: 19-March 11

Re: How to compile java project with multiple packages in linux?

Posted 17 August 2011 - 09:31 AM

I've got a simple default build file that I made when I was learning my way around ant. It's on my home machine, I'll try to remember to post it after I get home from playing tunes tonight. It'll give you a place to start from.
Was This Post Helpful? 1
  • +
  • -

#11 sam.bengtson  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 58
  • Joined: 21-February 11

Re: How to compile java project with multiple packages in linux?

Posted 17 August 2011 - 09:32 AM

ok thanks
Was This Post Helpful? 0
  • +
  • -

#12 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1155
  • View blog
  • Posts: 2,534
  • Joined: 05-May 05

Re: How to compile java project with multiple packages in linux?

Posted 17 August 2011 - 09:40 AM

I've recreated your problem with a simpler representation to try to help you understand how to fix it. Specifically, I'm going to use

Quote

AsteriskConnector/src/Asterisk/Main.java:4: package org.apache.axis does not exist
import org.apache.axis.AxisFault;


as an example.

Using a build tool may complicate matters even more. Here is a discussion on how to generate a build file in eclipse. Judging from your use of Apache libraries, it seems like your working on something that probably does require a makefile. My aim is to show you how to fix this simple problem without build tools, since you'll probably have to specify the same compilation commands in the makefile anyway.

Suppose I have the following two files on my desktop directory.

Main.java

import test.Foo;
public class Main {
     public static void main(String[] args) {
        
     }
}


Foo.java

package test;
class Foo{}



I issue the following commands on the command line ('#' denotes a comment):

javac Foo.java # compile Foo.java
mkdir test # create 'test' directory
cp Foo.class test # copy 'Foo.class' into 'test' directory



Here's what my directory structure looks like:

|<dir name>| denotes a directory


                 |Desktop|
          /          |           \
      Main.java    |test|      Foo.java & Foo.class
                     |
                    Foo.class



Then, I create a jar file with Foo.class and put it in another directory. This is equivalent of downloading axis.jar, which has org.apache.axis.AxisFault. I put test.jar into AnotherDir to mirror the fact that axis.jar resides in a different location than AsteriskConnector/src/Asterisk/Main.java.

jar -cf test.jar test # create 'test' jar file
mkdir AnotherDir 
cp test.jar AnotherDir



And, the structure becomes:

|<dir name>| denotes a directory


                 |Desktop|
          /          |           \
      Main.java    |test|      |AnotherDir|
                                   |
                                test.jar
                                   |
                                 |test|
                                   |
                                 Foo.class



This is basically what's going on with your project. I want to compile Main.java which imports test.Foo, but test.Foo resides in test.jar which resides in another directory.

So I compile Main.java:

javac Main.java 


And javac throws an error stating:

Quote

Main.java:1: error: package test does not exist
import test.Foo;
^
1 error


This is the exact mirror of the error you got which I stated above. It's because I didn't include test.jar in my class path. The correct command is:

javac -cp AnotherDir/test.jar Main.java


And now it works.

This post has been edited by blackcompe: 17 August 2011 - 09:50 AM

Was This Post Helpful? 2
  • +
  • -

#13 sam.bengtson  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 58
  • Joined: 21-February 11

Re: How to compile java project with multiple packages in linux?

Posted 17 August 2011 - 09:47 AM

Ok wow thanks! So if I need to include multiple jar files, I add them to the 'AnotherDir' directory and compile like this?
javac -cp AnotherDir/test.jar AnotherDir/test2.jar AnotherDir/test3.jar Main.java
?
Was This Post Helpful? 0
  • +
  • -

#14 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1155
  • View blog
  • Posts: 2,534
  • Joined: 05-May 05

Re: How to compile java project with multiple packages in linux?

Posted 17 August 2011 - 09:57 AM

Right. Well, in Windows, use a forward slash. To include multiple directories or jar files you have to append the paths onto one string.

In Linux it's:

javac -cp AnotherDir/test.jar:AnotherDir/test2.jar:AnotherDir/test3.jar Main.java


In Windows, I think it would be:

javac -cp AnotherDir\test.jar;AnotherDir\test2.jar;AnotherDir\test3.jar Main.java

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1