Kentor
Kentor

Reputation: 657

how to compile java files in linux with a package

I am able to compile my Main.java file without any issues in Eclipse.

Inside Main.java I have

package com.selenium.zLoadProfilerPkg;

Inside the other 3 files I also have

package com.selenium.zLoadProfilerPkg;

However when I upload the whole structure to linux, and I try to compile it, I get errors:

[sgalkov@zpub zLoadProfilerPkg]$ pwd
/home/sgalkov/zpp_tech_git_checkout/profiler/zLoadProfiler/zLoadProfiler/src/com/selenium/zLoadProfilerPkg
[sgalkov@zpub zLoadProfilerPkg]$ cd /home/sgalkov/zpp_tech_git_checkout/profiler/zLoadProfiler/zLoadProfiler/src/com/selenium/zLoadProfilerPkg/; javac -cp ".:/home/sgalkov/zpp_tech_git_checkout/profiler/selenium-2.30.0/selenium-java-2.30.0.jar:/home/sgalkov/zpp_tech_git_checkout/profiler/selenium-2.30.0/libs/*:/home/sgalkov/zpp_tech_git_checkout/profiler/selenium-2.30.0/selenium-server-standalone-2.30.0.jar:/home/sgalkov/zpp_tech_git_checkout/profiler/browsermob-proxy-2.0-beta-7/browsermob-proxy-2.0-beta-7-sources.jar:/home/sgalkov/zpp_tech_git_checkout/profiler/browsermob-proxy-2.0-beta-7/lib/*:/home/sgalkov/zpp_tech_git_checkout/profiler/zLoadProfiler/zLoadProfiler/src/com/selenium/zLoadProfilerPkg" Main.java
Main.java:54: error: cannot find symbol
    BrowsermobProxy bmp = new BrowsermobProxy(PROXY_API_HOST, PROXY_API_PORT);
    ^
symbol:   class BrowsermobProxy
location: class Main

Main.java:54: error: cannot find symbol
    BrowsermobProxy bmp = new BrowsermobProxy(PROXY_API_HOST, PROXY_API_PORT);
                              ^
symbol:   class BrowsermobProxy
location: class Main

Main.java:310: error: cannot find symbol
        HarStorage hs = new HarStorage(HARSTORAGE_HOST, HARSTORAGE_PORT);
        ^
symbol:   class HarStorage
location: class Main

Main.java:310: error: cannot find symbol
        HarStorage hs = new HarStorage(HARSTORAGE_HOST, HARSTORAGE_PORT);
                            ^
symbol:   class HarStorage
location: class Main

4 errors
[sgalkov@zpub zLoadProfilerPkg]$

If I comment out "package com.selenium.zLoadProfilerPkg;" in each of the 4 files and compile them one by one, everything works fine.

Also, If I try to compile it this way:

cd /home/sgalkov/zpp_tech_git_checkout/profiler/zLoadProfiler/zLoadProfiler/src/com/selenium/zLoadProfilerPkg/;

javac -cp ".\
:/home/sgalkov/zpp_tech_git_checkout/profiler/selenium-2.30.0/selenium-java-2.30.0.jar\
:/home/sgalkov/zpp_tech_git_checkout/profiler/selenium-2.30.0/libs/*\
:/home/sgalkov/zpp_tech_git_checkout/profiler/selenium-2.30.0/selenium-server-standalone-2.30.0.jar\
:/home/sgalkov/zpp_tech_git_checkout/profiler/browsermob-proxy-2.0-beta-7/browsermob-proxy-2.0-beta-7-sources.jar\
:/home/sgalkov/zpp_tech_git_checkout/profiler/browsermob-proxy-2.0-beta-7/lib/*\
:/home/sgalkov/zpp_tech_git_checkout/profiler/zLoadProfiler/zLoadProfiler/src/com/selenium/zLoadProfilerPkg" \
BrowsermobProxy.java HarStorage.java HttpRequest.java Main.java

it does compile but I am getting an extra class for Main.java

 rw-r--r-- 1 sgalkov users  1422 Mar 11 20:30 Main$1.class
 rw-r--r-- 1 sgalkov users  8264 Mar 11 20:30 Main.class
 rw-r--r-- 1 sgalkov users 14864 Mar 11 19:32 Main.java

and I am unable to run the program, I get the error:

 Exception in thread "main" java.lang.NoClassDefFoundError: Main (wrong name: com/selenium/zLoadProfilerPkg/Main)
    at java.lang.ClassLoader.defineClass1(Native Method)

I've looked around and tried various options but can't get this project to compile and would appreciate any pointers.

Upvotes: 0

Views: 1679

Answers (3)

Oleg Mikheev
Oleg Mikheev

Reputation: 17444

dir=/home/sgalkov/zpp_tech_git_checkout/profiler

cd "$dir/zLoadProfiler/zLoadProfiler/src/"

javac -cp ".\
:$dir/selenium-2.30.0/selenium-java-2.30.0.jar\
:$dir/selenium-2.30.0/libs/*\
:$dir/selenium-2.30.0/selenium-server-standalone-2.30.0.jar\
:$dir/browsermob-proxy-2.0-beta-7/browsermob-proxy-2.0-beta-7-sources.jar\
:$dir/browsermob-proxy-2.0-beta-7/lib/*\
:$dir/zLoadProfiler/zLoadProfiler/src/" \
com/selenium/zLoadProfilerPkg/*.java

Upvotes: 1

Joop Eggen
Joop Eggen

Reputation: 109613

You have to start javac in the source root directory where there is a subdirectory com (the top package). This you can test on Windows too.

You might deploy the compiled .class'es / jars / wars / ears too. Then the target of compilation is needed (1.5, 1.6, 1.7).

And yes, ant or nowadays maven might provide a good build infrastructure, both inside the IDE and standalone.

Upvotes: 0

cmevoli
cmevoli

Reputation: 351

If you want to compile your application outside of an IDE you should use Apache Ant. Here's an example build.xml file which you could use to compile your application with Ant.

<?xml version="1.0" encoding="UTF-8"?>
<project basedir="/home/sgalkov/zpp_tech_git_checkout/profiler/zLoadProfiler/zLoadProfiler"
    default="compile">

<property name="dir.prefix" value="/home/sgalkov/zpp_tech_git_checkout/profiler"/>

<path id="compile.classpath">
    <pathelement location="classes"/>
    <fileset dir="${dir.prefix}/selenium-2.30.0">
        <include name="selenium-java-2.30.0.jar"/>
        <include name="libs/*"/>
        <include name="selenium-server-standalone-2.30.0.jar"/>
    </fileset>
    <fileset dir="${dir.prefix}/browsermob-proxy-2.0-beta-7">
        <include name="browsermob-proxy-2.0-beta-7-sources.jar"/>
        <include name="lib/*"/>
    </fileset>
</path>
<property name="compile.classpath" refid="compile.classpath"/>


<target name="compile">
    <mkdir dir="classes"/>
    <javac failonerror="true" srcdir="src" debug="yes"
        includes="**/*.java" destdir="classes"
        classpath="${compile.classpath}">
    </javac>
</target>

<target name="clean">
    <delete dir="classes"/>
</target>

</project>

All your classes will end up in a folder called classes with a structure that mirrors your source package hierarchy. To execute your application, you can just place the classes folder in your classpath and call your main class as com.selenium.zLoadProfilerPkg.Main. There's some nice features in ant such as packaging your classes in a jar file, etc. Check it out.

As far as the Main$1.class, be sure your package definitions are correctly defined at the top of each of your java source files.

Also, be sure to check the compile.classpath in the ant build script I provided to ensure I didn't mispell or represent something from the classpath you provided in your question.

Upvotes: 0

Related Questions