Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Error trying to get a simple bare-bones package to work.

 
eileen keeney
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Earlier Today I was able to create a package, with a class, that had a method in it, and access the method from another program.
Now I can not even get a very simple case of a package to work, at all.
Even when I go back to the old code that worked, it fails to allow me to compile the code that calls the method in the package.


Code in file myTestClass.java
---
package mytestpack;

import java.io.*;
import java.util.*;
import java.lang.String;

public class myTestClass
{
public static int myTestMethod()
{
System.out.println("hello World");
int i=0;
return i;
}
}
----
I then do this
javac -d . myTestClass.java

It compiles, and creates directory mytestpack
In the directory it places myTestClass.class

Then I write a simple program to call the method.
Code for testpack.java
----
import java.io.*;
import java.util.*;
import java.lang.String;
import mytestpack.*;

class testmypack
{
public static void main(String[] args)
{
int i;
i= myTestClass.myTestMethod();
}
}

---
I attempt to compile the above using this command:
javac testpack.java

when I try to compile the above, I get this error:
testpack.java:11: cannot access myTestClass
bad class file: ./myTestClass.java
file does not contain class myTestClass
Please remove or make sure it appears in the correct subdirectory of the classpath.
i= myTestClass.myTestMethod();
^

Why is it even looking back at myTestClass.java, when I have already successfully compiled the class.
Would it not only need to access the myTestClass.class file?

The path to (and including) mytestpack is in my CLASSPATH.
(besides I get a different error, when it can't find the path).


What baffles me, is that I had this concept working earlier.
I am sure I used the same exact strategy as I show above.


If someone can tell me what I am doing wrong, it would be very much appreciated.
Thanks.
 
James Tharakan
Ranch Hand
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because the method in the myTestClass is static , you need to do static imports.
Replace...
import mytestpack.*; by import static mytestpack.*;

Learn about static imports for more details
 
eileen keeney
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That didn't solve the problem.

I got exactly the same error.

Taking static out of the method definition didn't work either.

Earlier, when I had something like this working, I did use public static for the method definition, did not use static in the import statement and it worked.
Now I can't get anything to work at all (except putting the methods all inside the main class, that needs to use them).
 
Saifuddin Merchant
Ranch Hand
Posts: 607
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
James Tharakan wrote:Because the method in the myTestClass is static , you need to do static imports.
Replace...
import mytestpack.*; by import static mytestpack.*;

Learn about static imports for more details


Static imports where only needed if you did not want to use the fully qualified class name...
Say PI instead of Math.PI. If you are using fully qualified class names you shouldn't really need static imports..

More ever Sun doesn't seem to recommend static imports anyways .... http://java.sun.com/j2se/1.5.0/docs/guide/language/static-import.html

As for the original question ... Something look wrong with class paths to me though I can't quite place it yet.
 
Saifuddin Merchant
Ranch Hand
Posts: 607
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok I stumped ... I've benn trying this out for 10 mins and its just not working from the command line. I put in in my eclipse IDE and it worked like a charm - so nothing wrong with the code.

Looking forward to a see whats going wrong here - Everything looks perfect too me
 
James Tharakan
Ranch Hand
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you try the actual code in command line with static imports???
 
Campbell Ritchie
Sheriff
Posts: 48441
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try adding the -d and . to every javac call.
 
eileen keeney
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is very strange.

I am now at work, and the same code that failed to compile using putty at home, is comiling without the error, on the unix workstation in the office.

Before when I said an example that had worked earlier for me, had just quit working; was working on my system at work, not at home.
I had then gone home, and continued working on it, when it started failing.

So what would cause this? (I guess we are getting beyond beginner here).

I have several java servlets, on the same server, that compile and run just fine, from my home system.
I don't see how this can be a firewall issue, but at the same time can not think of anything else.
 
eileen keeney
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another interesting symptom.

I get the same problem, when I compile from my PC, at work, using putty.
But it works using the unix workstation.

I get the error when I compile using reflection, on my PC as well.

Very very strange.
Any ideas on what would cause this?
 
John de Michele
Rancher
Posts: 600
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Eileen:

I got your program to work by changing the import in the second class from

to


I'm not sure why it works, but it worked for me. BTW, you don't have to do includes for java.lang.* classes, since they are included automatically.

John.
 
james dunster
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator





eileen keeney wrote:Earlier Today I was able to create a package, with a class, that had a method in it, and access the method from another program.
Now I can not even get a very simple case of a package to work, at all.
Even when I go back to the old code that worked, it fails to allow me to compile the code that calls the method in the package.


Code in file myTestClass.java
---
package mytestpack;

import java.io.*;
import java.util.*;
import java.lang.String;

public class myTestClass
{
public static int myTestMethod()
{
System.out.println("hello World");
int i=0;
return i;
}
}
----
I then do this
javac -d . myTestClass.java

It compiles, and creates directory mytestpack
In the directory it places myTestClass.class

Then I write a simple program to call the method.
Code for testpack.java
----
import java.io.*;
import java.util.*;
import java.lang.String;
import mytestpack.*;

class testmypack <-------------------------------------<< ??
{
public static void main(String[] args)
{
int i;
i= myTestClass.myTestMethod();
}
}

---
I attempt to compile the above using this command:
javac testpack.java

when I try to compile the above, I get this error:
testpack.java:11: cannot access myTestClass
bad class file: ./myTestClass.java
file does not contain class myTestClass
Please remove or make sure it appears in the correct subdirectory of the classpath.
i= myTestClass.myTestMethod();
^

Why is it even looking back at myTestClass.java, when I have already successfully compiled the class.
Would it not only need to access the myTestClass.class file?

The path to (and including) mytestpack is in my CLASSPATH.
(besides I get a different error, when it can't find the path).


What baffles me, is that I had this concept working earlier.
I am sure I used the same exact strategy as I show above.


If someone can tell me what I am doing wrong, it would be very much appreciated.
Thanks.

What is testmypack?



 
eileen keeney
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John de Michele wrote:Eileen:

I got your program to work by changing the import in the second class from

to


I'm not sure why it works, but it worked for me. BTW, you don't have to do includes for java.lang.* classes, since they are included automatically.

John.


I was pretty certain I had tested that before with no luck, but I tried it just now, and it seems to work.

This does not explain the difference in behavior I am seeing between the use of javac when I am using putty (or reflection) versus the use of javac from a unix workstation.
But it does solve the issue, and get my code working.

Thanks.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic