0

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.

3
  • Main$1.class is not an "extra" class, it's an inner class (usually). Do you have an inner class in Main.java? (Even an anonymous inner class?) Commented Mar 12, 2013 at 0:41
  • 1
    Also you'd typically write a small script to handle the classpath and such if you need to build this on a regular basis in a *nix shell. Or you could use ant/maven/etc. Another alternative (if you aren't building it regularly on *nix) would be to just (cough) export it from Eclipse (since you say it works in Eclipse, export as a JAR). Exporting from an IDE Is not a good habit to get into, but if you need to do it on a one-off or very infrequent basis it works. Commented Mar 12, 2013 at 0:44
  • thanks I found the inner class Commented Mar 12, 2013 at 18:58

3 Answers 3

1
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
Sign up to request clarification or add additional context in comments.

2 Comments

this works but I am still getting a Main$1.class file for some reason
sometimes the shortest answer is the best because Genius is simplicity :)
0

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.

Comments

0

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.

1 Comment

yes I agree, ant/maven are a good idea but I'm just trying to get this work first :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.